Class: PoroValidator::Validators::NumericValidator

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

Overview

Since:

  • 0.0.1

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



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/poro_validator/validators/numeric_validator.rb', line 4

def validate(attribute, value, options)
  message = options.fetch(:message, :numeric)

  begin
    Kernel.Integer(value.to_s)
    nil
  rescue
    errors.add(attribute, :integer)
    return
  end

  options.keys.each do |key|
    matcher = matchers(key)
    next if matcher.nil?
    unless matchers(key).call(value, options[key])
      errors.add(attribute, message, key => options[key])
    end
  end
end