Sha256: 6e72a171033eaf6eb0f42b495449730f391ceb275241479f8d513eb4d2a19f6b

Contents?: true

Size: 1.6 KB

Versions: 10

Compression:

Stored size: 1.6 KB

Contents

module DataMapper
  module Validations

    ##
    #
    # @author Dirkjan Bussink
    # @since  0.9
    class PrimitiveTypeValidator < GenericValidator
      def call(target)
        value    = target.validation_property_value(field_name)
        property = target.validation_property(field_name)

        return true if value.nil? || property.primitive?(value)

        error_message = @options[:message] || default_error(property)
        add_error(target, error_message, field_name)

        false
      end

      protected

      def default_error(property)
        ValidationErrors.default_error_message(:primitive, field_name, property.primitive)
      end

    end # class PrimitiveTypeValidator

    module ValidatesPrimitiveType

      extend Deprecate

      ##
      # Validates that the specified attribute is of the correct primitive type.
      #
      # @example [Usage]
      #   require 'dm-validations'
      #
      #   class Person
      #     include DataMapper::Resource
      #
      #     property :birth_date, Date
      #
      #     validates_primitive_type_of :birth_date
      #
      #     # a call to valid? will return false unless
      #     # the birth_date is something that can be properly
      #     # casted into a Date object.
      #   end
      def validates_primitive_type_of(*fields)
        opts = opts_from_validator_args(fields)
        add_validator_to_context(opts, fields, DataMapper::Validations::PrimitiveTypeValidator)
      end

      deprecate :validates_is_primitive, :validates_primitive_type_of

    end # module ValidatesPresent
  end # module Validations
end # module DataMapper

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
dm-validations-1.1.0 lib/dm-validations/validators/primitive_validator.rb
dm-validations-1.1.0.rc3 lib/dm-validations/validators/primitive_validator.rb
dm-validations-1.1.0.rc2 lib/dm-validations/validators/primitive_validator.rb
dm-validations-1.1.0.rc1 lib/dm-validations/validators/primitive_validator.rb
dm-validations-1.0.2 lib/dm-validations/validators/primitive_validator.rb
dm-validations-1.0.1 lib/dm-validations/validators/primitive_validator.rb
dm-validations-1.0.0 lib/dm-validations/validators/primitive_validator.rb
dm-validations-1.0.0.rc3 lib/dm-validations/validators/primitive_validator.rb
dm-validations-1.0.0.rc2 lib/dm-validations/validators/primitive_validator.rb
dm-validations-1.0.0.rc1 lib/dm-validations/validators/primitive_validator.rb