Module: Mint::Registry
- Extended by:
- Registry
- Included in:
- Registry
- Defined in:
- lib/minting/mint/registry/zeros.rb,
lib/minting/mint/registry/crypto.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
-
.crypto_currencies ⇒ Array<Currency>
Returns the list of built-in crypto currency definitions.
-
.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.
-
.register_all_crypto ⇒ Array<Currency>
Registers all built-in crypto currencies at once.
-
.register_crypto(*codes) ⇒ Array<Currency>
Registers one or more crypto currencies into the shared currency registry.
-
.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) ⇒ Object
- #detect_currency(input) ⇒ Object
- #symbol_shared?(symbol) ⇒ Boolean
Class Method Details
.crypto_currencies ⇒ Array<Currency>
Returns the list of built-in crypto currency definitions.
These are not registered by default — call register_crypto to opt in.
17 18 19 20 21 22 23 24 |
# File 'lib/minting/mint/registry/crypto.rb', line 17 def self.crypto_currencies @crypto_currencies || CRYPTO_MUTEX.synchronize do @crypto_currencies ||= begin path = File.join(File.('../../data', __dir__), 'crypto-currencies.yaml') YAML.load_file(path).map { |entry| Currency.new(**entry.transform_keys(&:to_sym)) }.freeze end end end |
.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 |
# File 'lib/minting/mint/registry/registry.rb', line 34 def self.currencies = @currencies |
.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 |
# 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 @symbols_list = nil currency end end |
.register_all_crypto ⇒ Array<Currency>
Registers all built-in crypto currencies at once.
Raises on the first duplicate — call rescue if idempotency is needed.
53 54 55 56 57 |
# File 'lib/minting/mint/registry/crypto.rb', line 53 def self.register_all_crypto crypto_currencies.map do |c| Currency.register(code: c.code, subunit: c.subunit, symbol: c.symbol, priority: c.priority) end end |
.register_crypto(*codes) ⇒ Array<Currency>
Registers one or more crypto currencies into the shared currency registry.
Raises on duplicate registration or unknown code — use rescue if
idempotent bulk registration is needed.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/minting/mint/registry/crypto.rb', line 35 def self.register_crypto(*codes) entries = crypto_currencies index = entries.each_with_index.to_h { |currency, index| [currency.code, index] } missing = codes.reject { |code| index.key?(code) } raise ArgumentError, "Unknown crypto code(s): #{missing.join(', ')}" unless missing.empty? codes.map do |code| c = entries[index[code]] Currency.register(code:, subunit: c.subunit, symbol: c.symbol, priority: c.priority) 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.
28 |
# File 'lib/minting/mint/registry/registry.rb', line 28 def self.world_currencies = @world_currencies |
.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 17 18 19 20 |
# File 'lib/minting/mint/registry/zeros.rb', line 11 def self.zero_for(currency) raise ArgumentError, "Expect a Currency param. (#{currency})" unless currency.is_a?(Currency) return @zeros[currency] if @zeros&.key?(currency) MUTEX.synchronize do @zeros ||= {} @zeros[currency] ||= Mint::Money.send(:new, 0r, currency) end end |
Instance Method Details
#currency_for_symbol(symbol) ⇒ Object
8 9 10 11 |
# File 'lib/minting/mint/registry/symbols.rb', line 8 def currency_for_symbol(symbol) sync_symbols @symbols_map[symbol] end |
#detect_currency(input) ⇒ Object
18 19 20 21 |
# File 'lib/minting/mint/registry/symbols.rb', line 18 def detect_currency(input) sync_symbols input.match(@symbols_regex) { |m| @symbols_map[m[0]] } end |
#symbol_shared?(symbol) ⇒ Boolean
13 14 15 16 |
# File 'lib/minting/mint/registry/symbols.rb', line 13 def symbol_shared?(symbol) sync_symbols @shared_symbols.include?(symbol) end |