Module: RSchema::Coercers::Boolean

Extended by:
Boolean
Included in:
Boolean
Defined in:
lib/rschema/coercers/boolean.rb

Constant Summary collapse

TRUTHY_STRINGS =
['on', '1', 'true', 'yes']
FALSEY_STRINGS =
['off', '0', 'false', 'no']

Instance Method Summary collapse

Instance Method Details

#build(schema) ⇒ Object


10
11
12
# File 'lib/rschema/coercers/boolean.rb', line 10

def build(schema)
  self
end

#call(value) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rschema/coercers/boolean.rb', line 14

def call(value)
  case value
  when true, false then Result.success(value)
  when nil then Result.success(false)
  when String
    case
    when TRUTHY_STRINGS.include?(value.downcase) then Result.success(true)
    when FALSEY_STRINGS.include?(value.downcase) then Result.success(false)
    else Result.failure
    end
  else Result.failure
  end
end