Module: Money::Helpers

Defined in:
lib/money/helpers.rb

Constant Summary collapse

DECIMAL_ZERO =
BigDecimal(0).freeze
MAX_DECIMAL =
21
STRIPE_SUBUNIT_OVERRIDE =
{
  'ISK' => 100,
  'UGX' => 100,
}.freeze

Class Method Summary collapse

Class Method Details

.value_to_currency(currency) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/money/helpers.rb', line 44

def value_to_currency(currency)
  case currency
  when Money::Currency, Money::NullCurrency
    currency
  when nil, ''
    default = Money.current_currency || Money.default_currency
    raise(Money::Currency::UnknownCurrency, 'missing currency') if default.nil? || default == ''
    value_to_currency(default)
  when 'xxx', 'XXX'
    Money::NULL_CURRENCY
  when String
    begin
      Currency.find!(currency)
    rescue Money::Currency::UnknownCurrency => error
      if Money.config.legacy_deprecations
        Money.deprecate(error.message)
        Money::NULL_CURRENCY
      else
        raise error
      end
    end
  else
    raise ArgumentError, "could not parse as currency #{currency.inspect}"
  end
end

.value_to_decimal(num) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/money/helpers.rb', line 16

def value_to_decimal(num)
  value =
    case num
    when Money
      num.value
    when BigDecimal
      num
    when nil, 0, ''
      DECIMAL_ZERO
    when Integer
      BigDecimal(num)
    when Float
      BigDecimal(num, Float::DIG)
    when Rational
      BigDecimal(num, MAX_DECIMAL)
    when String
      decimal = BigDecimal(num, exception: !Money.config.legacy_deprecations)
      return decimal if decimal

      Money.deprecate("using Money.new('#{num}') is deprecated and will raise an ArgumentError in the next major release")
      DECIMAL_ZERO
    else
      raise ArgumentError, "could not parse as decimal #{num.inspect}"
    end
  return DECIMAL_ZERO if value.sign == BigDecimal::SIGN_NEGATIVE_ZERO
  value
end