Class Index [+]

Quicksearch

DataMapper::Validations::NumericalityValidator

@author Guy van den Berg @since 0.9

Public Instance Methods

call(target) click to toggle source
    # File lib/dm-validations/validators/numeric_validator.rb, line 7
 7:       def call(target)
 8:         value = target.validation_property_value(field_name)
 9:         return true if optional?(value)
10: 
11:         errors = []
12: 
13:         validate_with(integer_only? ? :integer : :numeric, value, errors)
14: 
15:         add_errors(target, errors)
16: 
17:         # if the number is invalid, skip further tests
18:         return false if errors.any?
19: 
20:         [ :gt, :lt, :gte, :lte, :eq, :ne ].each do |validation_type|
21:           validate_with(validation_type, value, errors)
22:         end
23: 
24:         add_errors(target, errors)
25: 
26:         errors.empty?
27:       end

Private Instance Methods

add_errors(target, errors) click to toggle source
    # File lib/dm-validations/validators/numeric_validator.rb, line 44
44:       def add_errors(target, errors)
45:         return if errors.empty?
46: 
47:         if options.key?(:message)
48:           add_error(target, options[:message], field_name)
49:         else
50:           errors.each do |error_message|
51:             add_error(target, error_message, field_name)
52:           end
53:         end
54:       end
integer_only?() click to toggle source
    # File lib/dm-validations/validators/numeric_validator.rb, line 31
31:       def integer_only?
32:         options[:only_integer] || options.fetch(:integer_only, false)
33:       end
validate_eq(value, errors) click to toggle source
     # File lib/dm-validations/validators/numeric_validator.rb, line 121
121:       def validate_eq(value, errors)
122:         eq = options[:eq] || options[:equal] || options[:equals] || options[:exactly] || options[:equal_to]
123:         validate_with_comparison(value, :==, eq, :equal_to, errors)
124:       end
validate_gt(value, errors) click to toggle source
     # File lib/dm-validations/validators/numeric_validator.rb, line 105
105:       def validate_gt(value, errors)
106:         validate_with_comparison(value, :>, options[:gt] || options[:greater_than], :greater_than, errors)
107:       end
validate_gte(value, errors) click to toggle source
     # File lib/dm-validations/validators/numeric_validator.rb, line 113
113:       def validate_gte(value, errors)
114:         validate_with_comparison(value, :>=, options[:gte] || options[:greater_than_or_equal_to], :greater_than_or_equal_to, errors)
115:       end
validate_integer(value, errors) click to toggle source
    # File lib/dm-validations/validators/numeric_validator.rb, line 80
80:       def validate_integer(value, errors)
81:         validate_with_comparison(value_as_string(value), :=~, /\A[+-]?\d+\z/, :not_an_integer, errors)
82:       end
validate_lt(value, errors) click to toggle source
     # File lib/dm-validations/validators/numeric_validator.rb, line 109
109:       def validate_lt(value, errors)
110:         validate_with_comparison(value, :<, options[:lt] || options[:less_than], :less_than, errors)
111:       end
validate_lte(value, errors) click to toggle source
     # File lib/dm-validations/validators/numeric_validator.rb, line 117
117:       def validate_lte(value, errors)
118:         validate_with_comparison(value, :<=, options[:lte] || options[:less_than_or_equal_to], :less_than_or_equal_to, errors)
119:       end
validate_ne(value, errors) click to toggle source
     # File lib/dm-validations/validators/numeric_validator.rb, line 126
126:       def validate_ne(value, errors)
127:         validate_with_comparison(value, :==, options[:ne] || options[:not_equal_to], :not_equal_to, errors, true)
128:       end
validate_numeric(value, errors) click to toggle source
     # File lib/dm-validations/validators/numeric_validator.rb, line 84
 84:       def validate_numeric(value, errors)
 85:         precision = options[:precision]
 86:         scale     = options[:scale]
 87: 
 88:         regexp = if precision && scale
 89:           if precision > scale && scale == 0
 90:             /\A[+-]?(?:\d{1,#{precision}}(?:\.0)?)\z/
 91:           elsif precision > scale
 92:             /\A[+-]?(?:\d{1,#{precision - scale}}|\d{0,#{precision - scale}}\.\d{1,#{scale}})\z/
 93:           elsif precision == scale
 94:             /\A[+-]?(?:0(?:\.\d{1,#{scale}})?)\z/
 95:           else
 96:             raise ArgumentError, "Invalid precision #{precision.inspect} and scale #{scale.inspect} for #{field_name} (value: #{value.inspect} #{value.class})"
 97:           end
 98:         else
 99:           /\A[+-]?(?:\d+|\d*\.\d+)\z/
100:         end
101: 
102:         validate_with_comparison(value_as_string(value), :=~, regexp, :not_a_number, errors)
103:       end
validate_with(validation_type, value, errors) click to toggle source
    # File lib/dm-validations/validators/numeric_validator.rb, line 56
56:       def validate_with(validation_type, value, errors)
57:         send("validate_#{validation_type}", value, errors)
58:       end
validate_with_comparison(value, cmp, expected, error_message_name, errors, negated = false) click to toggle source
    # File lib/dm-validations/validators/numeric_validator.rb, line 60
60:       def validate_with_comparison(value, cmp, expected, error_message_name, errors, negated = false)
61:         return if expected.nil?
62: 
63:         # XXX: workaround for jruby. This is needed because the jruby
64:         # compiler optimizes a bit too far with magic variables like $~.
65:         # the value.send line sends $~. Inserting this line makes sure the
66:         # jruby compiler does not optimise here.
67:         # see http://jira.codehaus.org/browse/JRUBY-3765
68:         $~ = nil if RUBY_PLATFORM[/java/]
69: 
70:         comparison = value.send(cmp, expected)
71:         return if negated ? !comparison : comparison
72: 
73:         errors << ValidationErrors.default_error_message(
74:           error_message_name,
75:           field_name,
76:           expected
77:         )
78:       end
value_as_string(value) click to toggle source
    # File lib/dm-validations/validators/numeric_validator.rb, line 35
35:       def value_as_string(value)
36:         case value
37:           # Avoid Scientific Notation in Float to_s
38:           when Float      then value.to_d.to_s('F')
39:           when BigDecimal then value.to_s('F')
40:           else value.to_s
41:         end
42:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.