Module: Mint::Registry
- Extended by:
- Registry
- Included in:
- Registry
- Defined in:
- lib/minting/mint/registry/zeros.rb,
lib/minting/mint/registry/symbols.rb,
lib/minting/mint/registry/registry.rb,
lib/minting/mint/registry/registration.rb
Overview
:nodoc:
Class Method Summary collapse
-
.currencies ⇒ Hash{String => Currency}
private
Returns the frozen hash of all registered currencies (world + custom).
-
.register(code:, subunit: 0, symbol: '', priority: 0) ⇒ Currency
Registers a new currency, raising a KeyError if already registered.
-
.world_currencies ⇒ Hash{String => Currency}
private
Loads ISO world currencies from YAML file.
-
.zero_for(currency) ⇒ Money
private
Returns the cached zero-Money for a currency, creating it if needed.
Instance Method Summary collapse
-
#currency_for_symbol(symbol) ⇒ Currency?
private
Looks up a currency by its display symbol.
-
#detect_currency(input) ⇒ Currency?
private
Scans
inputfor registered currency symbols and returns the first match.
Class 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 frozen hash of all registered currencies (world + custom).
34 35 36 |
# File 'lib/minting/mint/registry/registry.rb', line 34 def self.currencies @currencies || MUTEX.synchronize { @currencies = world_currencies.dup.freeze } end |
.register(code:, subunit: 0, symbol: '', priority: 0) ⇒ Currency
Registers a new currency, raising a KeyError if already registered.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/minting/mint/registry/registration.rb', line 15 def self.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 MUTEX.synchronize do raise KeyError, "Currency: #{code} already registered" if currencies[code] currency = Currency.new(code:, subunit:, symbol:, priority:) @currencies = @currencies.merge(code => currency).freeze @currency_symbols = nil @currency_symbol_map = nil currency end end |
.world_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.
Loads ISO world currencies from YAML file.
21 22 23 24 25 26 27 28 |
# File 'lib/minting/mint/registry/registry.rb', line 21 def self.world_currencies @world_currencies || MUTEX.synchronize do @world_currencies = begin path = File.join(File.('../../data', __dir__), 'world-currencies.yaml') YAML.load_file(path).to_h { |entry| [entry['code'], Currency.new(**entry.transform_keys(&:to_sym))] } end.freeze end end |
.zero_for(currency) ⇒ Money
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 cached zero-Money for a currency, creating it if needed.
11 12 13 14 15 16 |
# File 'lib/minting/mint/registry/zeros.rb', line 11 def self.zero_for(currency) MUTEX.synchronize do @zeros ||= {} @zeros[currency] ||= Mint::Money.send(:new, 0, currency) end end |
Instance Method Details
#currency_for_symbol(symbol) ⇒ 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.
Looks up a currency by its display symbol.
13 14 15 16 |
# File 'lib/minting/mint/registry/symbols.rb', line 13 def currency_for_symbol(symbol) @currency_symbol_map || MUTEX.synchronize { @currency_symbol_map = currency_symbols.to_h.freeze } @currency_symbol_map[symbol] end |
#detect_currency(input) ⇒ 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.
Scans input for registered currency symbols and returns the first match.
23 24 25 26 27 28 |
# File 'lib/minting/mint/registry/symbols.rb', line 23 def detect_currency(input) currency_symbols.each do |symbol, currency| return currency if input.include?(symbol) end nil end |