Sha256: ab09666d74084a8dfb0e29456492c1fbda152c5367234248a948d97c2e761fa6

Contents?: true

Size: 780 Bytes

Versions: 3

Compression:

Stored size: 780 Bytes

Contents

module ActiveModel
  module Validations
    # Adds type validation to ActiveModel::Model.
    #
    # Usage:
    # @example
    #   class FooBar
    #     include ActiveModel::Model
    #
    #     attr_accessor :date
    #
    #     validates :date, type: Date
    #   end
    class TypeValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        classes = Array(options[:with] || options[:in])
        unless classes.any? { |klass| value.is_a? klass }
          allowed_classes = classes.to_sentence two_words_connector: " or ",
                                                last_word_connector: ", or "

          record.errors.add(attribute, (options[:message] || "must be a #{allowed_classes}"))
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ph_model-1.2.0 lib/active_model/validations/type_validator.rb
ph_model-1.1.3 lib/active_model/validations/type_validator.rb
ph_model-1.1.2 lib/active_model/validations/type_validator.rb