Sha256: f36bf9cb68e36491e12108775b1da8dde568b45c84a3097e976436f453b5956a

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 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.values.values.map(&:value)
          end

          args << options

          validates_inclusion_of(*args)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
protobuf-activerecord-3.1.0.alpha2 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.1.0.alpha lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.0.2 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.0.1 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.0.0 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.0.0.rc4 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.0.0.rc3 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.0.0.rc2 lib/protobuf/active_record/validations.rb
protobuf-activerecord-3.0.0.pre lib/protobuf/active_record/validations.rb