Class: HaveAPI::Client::Validators::Numericality

Inherits:
HaveAPI::Client::Validator show all
Defined in:
lib/haveapi/client/validators/numericality.rb

Instance Attribute Summary

Attributes inherited from HaveAPI::Client::Validator

#params, #value

Instance Method Summary collapse

Methods inherited from HaveAPI::Client::Validator

#errors, #initialize, name, register, validate

Constructor Details

This class inherits a constructor from HaveAPI::Client::Validator

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/haveapi/client/validators/numericality.rb', line 7

def valid?
  if value.is_a?(::String)
    return false if /\A\d+\z/ !~ value
    v = value.to_i

  else
    v = value
  end

  ret = true
  ret = false if opts[:min] && v < opts[:min]
  ret = false if opts[:max] && v > opts[:max]
  ret = false if opts[:step] && (v - (opts[:min] || 0)) % opts[:step] != 0
  ret = false if opts[:mod] && v % opts[:mod] != 0
  ret = false if opts[:odd] && v % 2 == 0
  ret = false if opts[:even] && v % 2 > 0
  ret
end