Class: Typed::Coercion::BooleanCoercer

Inherits:
Coercer
  • Object
show all
Extended by:
T::Generic
Defined in:
lib/typed/coercion/boolean_coercer.rb

Constant Summary collapse

Target =
type_member { {fixed: T::Boolean} }

Instance Method Summary collapse

Instance Method Details

#coerce(type:, value:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/typed/coercion/boolean_coercer.rb', line 16

def coerce(type:, value:)
  return Failure.new(CoercionError.new("Type must be a T::Boolean.")) unless used_for_type?(type)

  if type.recursively_valid?(value)
    Success.new(value)
  elsif value == "true"
    Success.new(true)
  elsif value == "false"
    Success.new(false)
  else
    Failure.new(CoercionError.new)
  end
end

#used_for_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/typed/coercion/boolean_coercer.rb', line 11

def used_for_type?(type)
  type == T::Utils.coerce(T::Boolean)
end