Module: CMDx::Coercions::Integer

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

Overview

Coerces to Integer via ‘Kernel#Integer` (strict; rejects floats-as-strings). Pass `base:` to parse strings written in non-decimal radix (e.g. `“0x10”` with `base: 16`). The `:base` option is applied only when `value` is a String — for numeric inputs `Kernel#Integer` is called without a base, matching its native contract.

Instance Method Summary collapse

Instance Method Details

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

Parameters:

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

Options Hash (options):

  • :base (Integer)

    radix for string parsing (default 10)

Returns:



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

def call(value, options = EMPTY_HASH)
  base = options[:base]
  if base && value.is_a?(::String)
    Integer(value, base)
  else
    Integer(value)
  end
rescue ArgumentError, FloatDomainError, RangeError, TypeError
  type = I18nProxy.t("cmdx.types.integer")
  Failure.new(I18nProxy.t("cmdx.coercions.into_an", type:))
end