Module: Candid::Internal::Types::Boolean

Extended by:
Union
Defined in:
lib/candid/internal/types/boolean.rb

Class Method Summary collapse

Methods included from Union

coerce, discriminant, member, member?, members

Methods included from Type

#coerce, #strict!, #strict?

Methods included from JSON::Serializable

#dump, #load

Class Method Details

.coerce(value, strict: strict?) ) ⇒ Object

Overrides the base coercion method for enums to allow integer and string values to become booleans

Parameters:

  • value (Object)
  • strict (Hash) (defaults to: strict?) )

    a customizable set of options

Options Hash (strict:):

Returns:

  • (Object)

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/candid/internal/types/boolean.rb', line 17

def self.coerce(value, strict: strict?)
  case value
  when TrueClass, FalseClass
    value
  when Integer
    return value == 1
  when String
    return %w[1 true].include?(value)
  end

  raise Errors::TypeError, "cannot coerce `#{value.class}` to Boolean" if strict

  value
end