Class Index [+]

Quicksearch

ActiveModel::Validations::NumericalityValidator

Constants

CHECKS
RESERVED_OPTIONS

Public Class Methods

new(options) click to toggle source
    # File lib/active_model/validations/numericality.rb, line 12
12:       def initialize(options)
13:         super(options.reverse_merge(:only_integer => false, :allow_nil => false))
14:       end

Public Instance Methods

check_validity!() click to toggle source
    # File lib/active_model/validations/numericality.rb, line 16
16:       def check_validity!
17:         keys = CHECKS.keys - [:odd, :even]
18:         options.slice(*keys).each do |option, value|
19:           next if value.is_a?(Numeric) || value.is_a?(Proc) || value.is_a?(Symbol)
20:           raise ArgumentError, ":#{option} must be a number, a symbol or a proc"
21:         end
22:       end
validate_each(record, attr_name, value) click to toggle source
    # File lib/active_model/validations/numericality.rb, line 24
24:       def validate_each(record, attr_name, value)
25:         before_type_cast = "#{attr_name}_before_type_cast"
26: 
27:         raw_value = record.send("#{attr_name}_before_type_cast") if record.respond_to?(before_type_cast.to_sym)
28:         raw_value ||= value
29: 
30:         return if options[:allow_nil] && raw_value.nil?
31: 
32:         unless value = parse_raw_value_as_a_number(raw_value)
33:           record.errors.add(attr_name, :not_a_number, filtered_options(raw_value))
34:           return
35:         end
36: 
37:         if options[:only_integer]
38:           unless value = parse_raw_value_as_an_integer(raw_value)
39:             record.errors.add(attr_name, :not_an_integer, filtered_options(raw_value))
40:             return
41:           end
42:         end
43: 
44:         options.slice(*CHECKS.keys).each do |option, option_value|
45:           case option
46:           when :odd, :even
47:             unless value.to_i.send(CHECKS[option])
48:               record.errors.add(attr_name, option, filtered_options(value))
49:             end
50:           else
51:             option_value = option_value.call(record) if option_value.is_a?(Proc)
52:             option_value = record.send(option_value) if option_value.is_a?(Symbol)
53: 
54:             unless value.send(CHECKS[option], option_value)
55:               record.errors.add(attr_name, option, filtered_options(value).merge(:count => option_value))
56:             end
57:           end
58:         end
59:       end

Protected Instance Methods

filtered_options(value) click to toggle source
    # File lib/active_model/validations/numericality.rb, line 80
80:       def filtered_options(value)
81:         options.except(*RESERVED_OPTIONS).merge!(:value => value)
82:       end
parse_raw_value_as_a_number(raw_value) click to toggle source
    # File lib/active_model/validations/numericality.rb, line 63
63:       def parse_raw_value_as_a_number(raw_value)
64:         case raw_value
65:         when /\A0[xX]/
66:           nil
67:         else
68:           begin
69:             Kernel.Float(raw_value)
70:           rescue ArgumentError, TypeError
71:             nil
72:           end
73:         end
74:       end
parse_raw_value_as_an_integer(raw_value) click to toggle source
    # File lib/active_model/validations/numericality.rb, line 76
76:       def parse_raw_value_as_an_integer(raw_value)
77:         raw_value.to_i if raw_value.to_s =~ /\A[+-]?\d+\Z/
78:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.