Module: CMDx::Coercions::Boolean

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

Overview

Converts various input types to Boolean format

Handles conversion from strings, numbers, and other values to boolean using predefined truthy and falsey patterns.

Constant Summary collapse

FALSEY =
/^(false|f|no|n|0)$/i
TRUTHY =
/^(true|t|yes|y|1)$/i

Instance Method Summary collapse

Instance Method Details

#call(value, options = {}) ⇒ Boolean

Converts a value to a Boolean

Examples:

Convert truthy strings to true

Boolean.call("true")   # => true
Boolean.call("yes")    # => true
Boolean.call("1")      # => true

Convert falsey strings to false

Boolean.call("false")  # => false
Boolean.call("no")     # => false
Boolean.call("0")      # => false

Handle case-insensitive input

Boolean.call("TRUE")   # => true
Boolean.call("False")  # => false

Parameters:

  • value (Object)

    The value to convert to boolean

  • options (Hash) (defaults to: {})

    Optional configuration parameters (currently unused)

Options Hash (options):

  • :unused (Object)

    Currently no options are used

Returns:

  • (Boolean)

    The converted boolean value

Raises:

  • (CoercionError)

    If the value cannot be converted to boolean



42
43
44
45
46
47
48
49
50
# File 'lib/cmdx/coercions/boolean.rb', line 42

def call(value, options = {})
  case value.to_s.downcase
  when FALSEY then false
  when TRUTHY then true
  else
    type = Locale.t("cmdx.types.boolean")
    raise CoercionError, Locale.t("cmdx.coercions.into_a", type:)
  end
end