Sha256: 2cdd33fc4924ed74c8b3f3e26893c6e82c25d84e63ce56209b010b9424d91e98

Contents?: true

Size: 1.21 KB

Versions: 24

Compression:

Stored size: 1.21 KB

Contents

require 'active_support/concern'

module Protobuf
  module ActiveRecord
    module Validations
      extend ::ActiveSupport::Concern

      module ClassMethods
        # Validates whether the value of the specified attribute is available in
        # the given Protobuf Enum. The enumeration should be passed as a class
        # that defines the enumeration:
        #
        # ```
        # class User < ActiveRecord::Base
        #   include ::Protoable
        #
        #   validates_enumeration_of :role_type, :with => RoleType, :allow_nil => true
        # end
        # ```
        #
        # In this example, RoleType is a defined as a protobuf enum.
        #
        # It accepts the same options as `validates_inclusion_of` (the :in option
        # is automatically set and will be overwritten).
        #
        def validates_enumeration_of(*args)
          options = args.extract_options!
          enumerable = options.delete(:with)

          raise ArgumentError, ":with must be specified" if enumerable.nil?

          if enumerable < ::Protobuf::Enum
            options[:in] = enumerable.all_tags
          end

          args << options

          validates_inclusion_of(*args)
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
protobuf-activerecord-3.6.0 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.5.1 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.5.0 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.4.4 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.4.4.pre lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.4.3 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.4.2 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.4.1 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.4.0 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.3.8 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.3.7 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.3.6 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.3.5 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.3.4 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.3.3 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.3.2 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.3.1 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.3.0 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.2.2 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.2.1 lib/protobuf/active_record/validations.rb