Class: AWS::Record::NumericalityValidator
- Defined in:
- lib/aws/record/validators/numericality.rb
Constant Summary collapse
- ACCEPTED_OPTIONS =
[ :greater_than, :greater_than_or_equal_to, :less_than, :less_than_or_equal_to, :equal_to, :only_integer, :odd, :even, :message, :allow_nil, :on, :if, :unless, ]
- COMPARISONS =
{ :equal_to => :==, :greater_than => :>, :greater_than_or_equal_to => :>=, :less_than => :<, :less_than_or_equal_to => :<=, :even => lambda{|value| value.to_i % 2 == 0 }, :odd => lambda{|value| value.to_i % 2 == 1 }, }
Instance Attribute Summary
Attributes inherited from Validator
Instance Method Summary collapse
- #as_integer(value) ⇒ Object
- #as_number(value) ⇒ Object
- #message_for(error_type, requirement = nil) ⇒ Object
- #read_attribute_for_validation(record, attribute_name) ⇒ Object
- #setup(record_class) ⇒ Object
- #validate_attribute(record, attribute_name, raw) ⇒ Object
Methods inherited from Validator
Constructor Details
This class inherits a constructor from AWS::Record::Validator
Instance Method Details
#as_integer(value) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/aws/record/validators/numericality.rb', line 127 def as_integer value begin Kernel.Integer(value) rescue ArgumentError, TypeError nil end end |
#as_number(value) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/aws/record/validators/numericality.rb', line 119 def as_number value begin Kernel.Float(value) rescue ArgumentError, TypeError nil end end |
#message_for(error_type, requirement = nil) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/aws/record/validators/numericality.rb', line 106 def error_type, requirement = nil return [:message] if [:message] case error_type when :not_a_number then 'is not a number' when :not_an_integer then 'must be an integer' when :even then 'must be an even number' when :odd then 'must be an odd number' when :equal_to then "should equal #{requirement}" else "must be #{error_type.to_s.gsub(/_/, ' ')} #{requirement}" end end |
#read_attribute_for_validation(record, attribute_name) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/aws/record/validators/numericality.rb', line 58 def read_attribute_for_validation(record, attribute_name) if record.respond_to?("#{attribute_name}_before_type_cast") record.send("#{attribute_name}_before_type_cast") else record.send(attribute_name) end end |
#setup(record_class) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/aws/record/validators/numericality.rb', line 39 def setup record_class ensure_exclusive(:odd, :even) ensure_exclusive(:equal_to, [:greater_than, :greater_than_or_equal_to, :less_than, :less_than_or_equal_to]) ensure_type([TrueClass, FalseClass], :only_integer) ensure_type(TrueClass, :odd, :even) ensure_type([Numeric, Symbol, Proc], :greater_than, :greater_than_or_equal_to, :less_than, :less_than_or_equal_to, :equal_to) end |
#validate_attribute(record, attribute_name, raw) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/aws/record/validators/numericality.rb', line 66 def validate_attribute record, attribute_name, raw each_value(raw) do |raw_value| if [:only_integer] or [:odd] or [:even] value = as_integer(raw_value) error_type = :not_an_integer else value = as_number(raw_value) error_type = :not_a_number end unless value record.errors.add(attribute_name, (error_type)) return end COMPARISONS.each do |option,method| next unless .has_key?(option) requirement = case [option] when Symbol then record.send([option]) when Proc then [option].call(record) else [option] end valid = case method when Symbol then value.send(method, requirement) else method.call(value) end unless valid = (option, requirement) record.errors.add(attribute_name, ) end end end end |