Class: PoroValidator::Validators::NumericValidator
- Defined in:
- lib/poro_validator/validators/numeric_validator.rb
Overview
Direct Known Subclasses
Constant Summary collapse
- INTEGER_MATCHER =
/\A[-+]?(\d+|\d{1,3}(,\d{3})*)\z/.freeze
- FLOAT_MATCHER =
/\A[-+]?(\d+|\d{1,3}(,\d{3})*)(\.\d+)\z/.freeze
- VALID_OPTIONS =
[ :extremum, :max, :min, :in ].freeze
- VALID_OPTION_VALUES =
[ ::Range, ::Array, ::Fixnum, ::Numeric ].freeze
Instance Attribute Summary
Attributes inherited from BaseClass
Instance Method Summary collapse
Methods inherited from BaseClass
#__validate__, #context, #errors, #initialize, #nested?, #options, #value
Constructor Details
This class inherits a constructor from PoroValidator::Validators::BaseClass
Instance Method Details
#validate(attribute, value, options) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/poro_validator/validators/numeric_validator.rb', line 15 def validate(attribute, value, ) return if value.nil? unless is_numeric?(value.to_s, INTEGER_MATCHER) || is_numeric?(value.to_s, FLOAT_MATCHER) errors.add(attribute, :integer_or_float) return end = [:message] || :numeric (.keys) .each do |k, v| unless VALID_OPTION_VALUES.include?(v.class) raise ::PoroValidator::InvalidValidator.new( "Invalid option: #{k} => #{v}" ) end unless matchers(k).call(value, v) errors.add(attribute, , k => v) end end end |