Module: Mint::CurrencyRegistry

Extended by:
CurrencyRegistry
Included in:
CurrencyRegistry
Defined in:
lib/minting/mint/currency/currency_registry.rb

Overview

Internal currency registry Manages the registry cache and currency symbol lookups.

Instance Method Summary collapse

Instance Method Details

#currenciesHash{String => Currency}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the hash of all registered currencies.

Returns:

  • (Hash{String => Currency})

    registered currencies mapped by code



16
17
18
# File 'lib/minting/mint/currency/currency_registry.rb', line 16

def currencies
  @currencies ||= Mint.world_currencies.dup
end

#currency_symbolsArray<Array<String, Currency>>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Array<Array<String, Currency>>)

    sorted symbol-to-currency mappings



24
25
26
27
28
29
30
31
# File 'lib/minting/mint/currency/currency_registry.rb', line 24

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

#register(code:, subunit: 0, symbol: '', priority: 0) ⇒ Currency

Registers a new currency, raising a KeyError if already registered.

Parameters:

  • code (String)

    the unique currency code

  • subunit (Integer) (defaults to: 0)

    the decimal subunit precision, defaults to 0

  • symbol (String) (defaults to: '')

    the display symbol

  • priority (Integer) (defaults to: 0)

    parser precedence priority

Returns:

  • (Currency)

    the newly registered Currency instance

Raises:

  • (ArgumentError)

    if the code contains invalid characters

  • (KeyError)

    if the currency code is already registered



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

def register(code:, subunit: 0, symbol: '', priority: 0)
  raise ArgumentError, 'Currency code must be String' unless code.is_a? String
  unless code.match?(/^[A-Z_]+$/)
    raise ArgumentError,
          "Currency code must have only letters or '_' ('USD',, 'MY_COIN')"
  end

  currencies = CurrencyRegistry.currencies
  raise KeyError, "Currency: #{code} already registered" if currencies[code]

  currency = currencies[code] = Currency.new(code:, subunit:, symbol:, priority:)
  invalidate_symbols_cache
  currency
end