Module: CMDx::Coercions::Integer
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
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cmdx/coercions/integer.rb', line 18 def call(value, = EMPTY_HASH) base = [: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 |