Class: PoroValidator::Validators::NumericValidator

Inherits:
BaseClass
  • Object
show all
Defined in:
lib/poro_validator/validators/numeric_validator.rb

Overview

Since:

  • 0.0.1

Direct Known Subclasses

FloatValidator, IntegerValidator

Constant Summary collapse

INTEGER_MATCHER =

Since:

  • 0.0.1

/\A[-+]?(\d+|\d{1,3}(,\d{3})*)\z/.freeze
FLOAT_MATCHER =

Since:

  • 0.0.1

/\A[-+]?(\d+|\d{1,3}(,\d{3})*)(\.\d+)\z/.freeze
VALID_OPTIONS =

Since:

  • 0.0.1

[
  :extremum, :max, :min, :in
].freeze
VALID_OPTION_VALUES =

Since:

  • 0.0.1

[
  ::Range, ::Array, ::Fixnum, ::Numeric
].freeze

Instance Attribute Summary

Attributes inherited from BaseClass

#attribute

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

Since:

  • 0.0.1



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, options)
  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 = options[:message] || :numeric

  validate_numeric_options(options.keys)

  options.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, message, k => v)
    end
  end
end