Class: Attributor::Boolean

Inherits:
Object
  • Object
show all
Includes:
Type
Defined in:
lib/attributor/types/boolean.rb

Class Method Summary collapse

Methods included from Type

included

Class Method Details

.example(_context = nil, options: {}) ⇒ Object



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

def self.example(_context = nil, options: {})
  [true, false].sample
end

.familyObject



26
27
28
# File 'lib/attributor/types/boolean.rb', line 26

def self.family
  'boolean'
end

.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object

Raises:



17
18
19
20
21
22
23
24
# File 'lib/attributor/types/boolean.rb', line 17

def self.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options)
  return nil if value.nil?

  raise CoercionError, context: context, from: value.class, to: self, value: value if value.is_a?(::Float)
  return false if [false, 'false', 'FALSE', '0', 0, 'f', 'F'].include?(value)
  return true if [true, 'true', 'TRUE', '1', 1, 't', 'T'].include?(value)
  raise CoercionError, context: context, from: value.class, to: self
end

.valid_type?(value) ⇒ Boolean

Returns:



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

def self.valid_type?(value)
  value == true || value == false
end