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). Anything else (including ‘nil`) 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
# File 'lib/cmdx/coercions/boolean.rb', line 19

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

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

  coercion_failure
end