Class: Rbdantic::Validators::Types::Boolean

Inherits:
Base
  • Object
show all
Defined in:
lib/rbdantic/validators/types/boolean.rb

Instance Attribute Summary

Attributes inherited from Base

#constraints

Instance Method Summary collapse

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
25
26
27
28
29
30
31
32
33
# File 'lib/rbdantic/validators/types/boolean.rb', line 17

def coerce(value)
  return value if value == true || value == false

  case value
  when ::String
    str = value.strip.downcase
    return true if str == "true" || str == "1" || str == "yes" || str == "on"
    return false if str == "false" || str == "0" || str == "no" || str == "off"
    nil
  when ::Integer
    return true if value == 1
    return false if value == 0
    nil
  else
    nil
  end
end

#expected_type_nameObject



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

def expected_type_name
  "Boolean"
end

#matches_type?(value) ⇒ Boolean

Returns:



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

def matches_type?(value)
  value.is_a?(TrueClass) || value.is_a?(FalseClass)
end