Class: Rbdantic::Validators::Types::Integer

Inherits:
Number
  • Object
show all
Defined in:
lib/rbdantic/validators/types/integer.rb

Constant Summary

Constants inherited from Number

Number::FLOAT_TOLERANCE_FACTOR, Number::MIN_FLOAT_TOLERANCE

Instance Attribute Summary

Attributes inherited from Base

#constraints

Instance Method Summary collapse

Methods inherited from Number

#validate_constraints

Methods inherited from Base

#initialize, #validate, #validate_constraints

Constructor Details

This class inherits a constructor from Rbdantic::Validators::Types::Base

Instance Method Details

#coerce(value) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/rbdantic/validators/types/integer.rb', line 17

def coerce(value)
  case value
  when ::Integer then value
  when ::Float then value.to_i if value == value.floor
  when ::String then Integer(value) rescue nil
  else nil
  end
end

#expected_type_nameObject



13
14
15
# File 'lib/rbdantic/validators/types/integer.rb', line 13

def expected_type_name
  "Integer"
end

#matches_type?(value) ⇒ Boolean

Returns:



9
10
11
# File 'lib/rbdantic/validators/types/integer.rb', line 9

def matches_type?(value)
  value.is_a?(::Integer)
end