Module: Mint

Defined in:
lib/minting/mint/registry.rb,
lib/minting/money/money.rb,
lib/minting/mint/currency.rb,
lib/minting/money/coercion.rb,
lib/minting/mint/refinements.rb,
lib/minting/money/allocation.rb,
lib/minting/money/comparable.rb,
lib/minting/money/conversion.rb,
lib/minting/money/arithmetics.rb

Overview

Mint is a library to operate with monetary values

Defined Under Namespace

Classes: Currency, Money

Class Method Summary collapse

Class Method Details

.currenciesObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/minting/mint/registry.rb', line 40

def self.currencies
  @currencies ||= {
    'AUD' => Currency.new('AUD', subunit: 2, symbol: '$'),
    'BRL' => Currency.new('BRL', subunit: 2, symbol: 'R$'),
    'CAD' => Currency.new('CAD', subunit: 2, symbol: 'R$'),
    'CHF' => Currency.new('CHF', subunit: 2, symbol: 'Fr'),
    'CNY' => Currency.new('CNY', subunit: 2, symbol: '¥'),
    'EUR' => Currency.new('EUR', subunit: 2, symbol: ''),
    'GBP' => Currency.new('GBP', subunit: 2, symbol: '£'),
    'JPY' => Currency.new('JPY', subunit: 0, symbol: '¥'),
    'MXN' => Currency.new('MXN', subunit: 2, symbol: '$'),
    'NZD' => Currency.new('NZD', subunit: 2, symbol: '$'),
    'PEN' => Currency.new('PEN', subunit: 2, symbol: 'S/.'),
    'SEK' => Currency.new('SEK', subunit: 2, symbol: 'kr'),
    'USD' => Currency.new('USD', subunit: 2, symbol: '$')
  }
end

.currency(currency) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/minting/mint/registry.rb', line 9

def self.currency(currency)
  case currency
  when Currency
    currency
  when Symbol
    currencies[currency.to_s]
  else
    currencies[currency]
  end
end

.money(amount, currency_code) ⇒ Object



5
6
7
# File 'lib/minting/mint/registry.rb', line 5

def self.money(amount, currency_code)
  Money.new(amount, currency(currency_code))
end

.register_currency(code, subunit: 2, symbol: '$') ⇒ Object



20
21
22
23
# File 'lib/minting/mint/registry.rb', line 20

def self.register_currency(code, subunit: 2, symbol: '$')
  code = code.to_s
  currencies[code] || register_currency!(code, subunit: subunit, symbol: symbol)
end

.register_currency!(code, subunit:, symbol: '') ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/minting/mint/registry.rb', line 25

def self.register_currency!(code, subunit:, symbol: '')
  code = code.to_s
  unless code.match?(/^[A-Z_]+$/)
    raise ArgumentError,
          "Currency code must be String or Symbol ('USD', :EUR)"
  end
  if currencies[code]
    raise KeyError,
          "Currency: #{code} already registered"
  end

  currencies[code] =
    Currency.new(code, subunit: subunit.to_i, symbol: symbol.to_s).freeze
end