Sha256: 2531c50bacbc5a55c69aad1409376b0c41d9b500e10213377e81bf040afdb80b

Contents?: true

Size: 1.65 KB

Versions: 7

Compression:

Stored size: 1.65 KB

Contents

module DataMapper
  module Validate

    ##
    #
    # @author Guy van den Berg
    # @since  0.9
    class WithinValidator < GenericValidator

      def initialize(field_name, options={})
        super
        @field_name, @options = field_name, options
        @options[:set] = [] unless @options.has_key?(:set)
      end

      def call(target)
        includes = @options[:set].include?(target.send(field_name))
        return true if includes

        field_name = Extlib::Inflection.humanize(@field_name)
        if @options[:set].is_a?(Range)
          if @options[:set].first != -n && @options[:set].last != n
            error_message = @options[:message] || "%s must be between %s and %s".t(field_name, @options[:set].first, @options[:set].last)
          elsif @options[:set].first == -n
            error_message = @options[:message] || "%s must be less than %s".t(field_name, @options[:set].last)
          elsif @options[:set].last == n
            error_message = @options[:message] || "%s must be greater than %s".t(field_name, @options[:set].first)
          end
        else
          error_message = "%s must be one of [%s]".t(field_name, @options[:set].join(', '))
        end

        add_error(target, error_message , @field_name)
        return false
      end

      def n
        1.0/0
      end
    end # class WithinValidator

    module ValidatesWithin

      # Validate the absence of a field
      #
      def validates_within(*fields)
        opts = opts_from_validator_args(fields)
        add_validator_to_context(opts, fields, DataMapper::Validate::WithinValidator)
      end

    end # module ValidatesWithin
  end # module Validate
end # module DataMapper

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
dm-validations-0.9.6 lib/dm-validations/within_validator.rb
dm-validations-0.9.9 lib/dm-validations/within_validator.rb
dm-validations-0.9.8 lib/dm-validations/within_validator.rb
dm-validations-0.9.7 lib/dm-validations/within_validator.rb
mack-data_mapper-0.8.2 lib/gems/dm-validations-0.9.7/lib/dm-validations/within_validator.rb
mack-data_mapper-0.8.3.1 lib/gems/dm-validations-0.9.9/lib/dm-validations/within_validator.rb
mack-data_mapper-0.8.3 lib/gems/dm-validations-0.9.9/lib/dm-validations/within_validator.rb