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
-
#currencies ⇒ Hash{String => Currency}
private
Returns the hash of all registered currencies.
-
#currency_symbols ⇒ Array<Array<String, Currency>>
private
Registered symbols sorted for detection: longest match wins, then parser priority.
-
#register(code:, subunit: 0, symbol: '', priority: 0) ⇒ Currency
Registers a new currency, raising a KeyError if already registered.
Instance Method Details
#currencies ⇒ Hash{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.
16 17 18 |
# File 'lib/minting/mint/currency/currency_registry.rb', line 16 def currencies @currencies ||= Mint.world_currencies.dup end |
#currency_symbols ⇒ Array<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.
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.
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 |