Module: CMDx::Coercions::Integer

Extended by:
Integer
Included in:
Integer
Defined in:
lib/cmdx/coercions/integer.rb

Overview

Converts various input types to Integer format

Handles conversion from strings, numbers, and other values to integers using Ruby’s Integer() method. Raises CoercionError for values that cannot be converted to integers.

Instance Method Summary collapse

Instance Method Details

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

Converts a value to an Integer

Examples:

Convert numeric strings to integers

Integer.call("42")      # => 42
Integer.call("-123")    # => -123
Integer.call("0")       # => 0

Convert numeric types to integers

Integer.call(42.0)      # => 42
Integer.call(3.14)      # => 3
Integer.call(0.0)       # => 0

Handle edge cases

Integer.call("")        # => 0
Integer.call(nil)       # => 0
Integer.call(false)     # => 0
Integer.call(true)      # => 1

Parameters:

  • value (Object)

    The value to convert to an integer

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

    Optional configuration parameters (currently unused)

Options Hash (options):

  • :unused (Object)

    Currently no options are used

Returns:

  • (Integer)

    The converted integer value

Raises:

  • (CoercionError)

    If the value cannot be converted to an integer



39
40
41
42
43
44
# File 'lib/cmdx/coercions/integer.rb', line 39

def call(value, options = {})
  Integer(value)
rescue ArgumentError, FloatDomainError, RangeError, TypeError
  type = Locale.t("cmdx.types.integer")
  raise CoercionError, Locale.t("cmdx.coercions.into_an", type:)
end