Sha256: f9b33a8c463e51c2647f9228d08cda0d0d9e7d0ea89a855195c88039992732b7

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

module DataMapper
  module Validations

    ##
    #
    # @author Guy van den Berg
    # @since  0.9
    class AbsenceValidator < GenericValidator
      def call(target)
        value = target.validation_property_value(field_name)
        return true if DataMapper::Ext.blank?(value)

        error_message = self.options[:message] || ValidationErrors.default_error_message(:absent, field_name)
        add_error(target, error_message, field_name)

        false
      end
    end # class AbsenceValidator

    module ValidatesAbsence

      extend Deprecate

      ##
      # Validates that the specified attribute is "blank" via the attribute's
      # #blank? method.
      #
      # @note
      #   dm-core's support lib adds the #blank? method to many classes,
      # @see lib/dm-core/support/blank.rb (dm-core) for more information.
      #
      # @example [Usage]
      #   require 'dm-validations'
      #
      #   class Page
      #     include DataMapper::Resource
      #
      #     property :unwanted_attribute, String
      #     property :another_unwanted, String
      #     property :yet_again, String
      #
      #     validates_absence_of :unwanted_attribute
      #     validates_absence_of :another_unwanted, :yet_again
      #
      #     # a call to valid? will return false unless
      #     # all three attributes are blank
      #   end
      #
      def validates_absence_of(*fields)
        opts = opts_from_validator_args(fields)
        add_validator_to_context(opts, fields, DataMapper::Validations::AbsenceValidator)
      end

      deprecate :validates_absent, :validates_absence_of

    end # module ValidatesAbsent
  end # module Validations
end # module DataMapper

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dm-validations-1.1.0 lib/dm-validations/validators/absent_field_validator.rb
dm-validations-1.1.0.rc3 lib/dm-validations/validators/absent_field_validator.rb