Module: CMDx::Coercions::Boolean

Extended by:
Boolean
Included in:
Boolean
Defined in:
lib/cmdx/coercions/boolean.rb

Overview

Coerces to Boolean by matching the string form against the TRUTHY and FALSEY sets (case- and whitespace-insensitive). ‘nil` becomes `false`; anything else unrecognized fails.

Constant Summary collapse

TRUTHY =
Set["true", "yes", "on", "y", "1", "t"].freeze
FALSEY =
Set["false", "no", "off", "n", "0", "f"].freeze

Instance Method Summary collapse

Instance Method Details

#call(value, options = EMPTY_HASH) ⇒ Boolean, Coercions::Failure

Parameters:

  • value (Object)
  • options (Hash{Symbol => Object}) (defaults to: EMPTY_HASH)

Options Hash (options):

  • reserved (Object)

    for future per-coercion configuration (currently ignored)

Returns:



19
20
21
22
23
24
25
26
27
28
# File 'lib/cmdx/coercions/boolean.rb', line 19

def call(value, options = EMPTY_HASH)
  return false if value.nil?

  str = value.to_s.strip.downcase
  return true if TRUTHY.include?(str)
  return false if FALSEY.include?(str)

  type = I18nProxy.t("cmdx.types.boolean")
  Failure.new(I18nProxy.t("cmdx.coercions.into_a", type:))
end