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 = EMPTY_HASH) ⇒ 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("")        # => raises CoercionError
Integer.call(nil)       # => raises CoercionError
Integer.call("abc")     # => raises CoercionError

Parameters:

  • value (Object)

    The value to convert to an integer

  • options (Hash) (defaults to: EMPTY_HASH)

    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

Rbs:

  • (untyped value, ?Hash[Symbol, untyped] options) -> Integer



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

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