Module: Mint

Defined in:
lib/minting/mint/registry.rb,
lib/minting/money/money.rb,
lib/minting/money/parse.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/formatting.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



46
47
48
# File 'lib/minting/mint/registry.rb', line 46

def self.currencies
  @currencies ||= load_currencies
end

.currency(currency) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/minting/mint/registry.rb', line 12

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

.currency_symbolsObject

Registered symbols sorted for detection: longest match wins, then parser priority.



51
52
53
54
55
56
57
58
# File 'lib/minting/mint/registry.rb', line 51

def self.currency_symbols
  @currency_symbols ||= begin
    currencies.values
              .map { |currency| [currency.symbol, currency] }
              .reject { |symbol, _| symbol.empty? }
              .sort_by { |symbol, currency| [-symbol.length, -currency.priority] }
  end.freeze
end

.money(amount, currency_code) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.money(amount, currency_code)
  currency = currency(currency_code)
  return Money.new(amount, currency) if currency

  raise ArgumentError, "Currency [#{currency_code}] not registered. Check Mint.currencies"
end

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



23
24
25
26
27
# File 'lib/minting/mint/registry.rb', line 23

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

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/minting/mint/registry.rb', line 29

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

  currencies[code] =
    Currency.new(code, subunit: subunit, symbol: symbol, priority: priority)
  @currency_symbols = nil
  currencies[code]
end