Module: CMDx::Coercions::BigDecimal

Extended by:
BigDecimal
Included in:
BigDecimal
Defined in:
lib/cmdx/coercions/big_decimal.rb

Overview

Coerces to ‘BigDecimal`. Default precision is 14 digits; override with `precision:` on the declaration.

Instance Method Summary collapse

Instance Method Details

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

Parameters:

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

Options Hash (options):

  • :precision (Integer) — default: 14

Returns:



15
16
17
18
19
20
21
22
# File 'lib/cmdx/coercions/big_decimal.rb', line 15

def call(value, options = EMPTY_HASH)
  return value if value.is_a?(BigDecimal)

  BigDecimal(value, options[:precision] || 14)
rescue ArgumentError, TypeError
  type = I18nProxy.t("cmdx.types.big_decimal")
  Failure.new(I18nProxy.t("cmdx.coercions.into_a", type:))
end