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,
lib/minting/money/constructors.rb,
lib/minting/mint/currency_store.rb
Overview
Mint currency store (internal)
Defined Under Namespace
Modules: CurrencyStore Classes: Currency, Money
Class Method Summary collapse
-
.currency(currency) ⇒ Currency?
Finds a registered currency by its code, symbol, or retrieves it directly if already a Currency object.
-
.currency_symbols ⇒ Array<Array<String, Currency>>
private
Registered symbols sorted for detection: longest match wins, then parser priority.
-
.money(amount, currency_code) ⇒ Money
Creates a new Money instance with the given amount and currency code.
-
.register_currency(code:, subunit: 0, symbol: '', priority: 0) ⇒ Currency
Registers a new currency if not already registered.
-
.register_currency!(code:, subunit:, symbol: '', priority: 0) ⇒ Currency
Strictly registers a new currency, raising a KeyError if already registered.
Class Method Details
.currency(currency) ⇒ Currency?
Finds a registered currency by its code, symbol, or retrieves it directly if already a Currency object.
23 24 25 |
# File 'lib/minting/mint/registry.rb', line 23 def self.currency(currency) currency.is_a?(Currency) ? currency : CurrencyStore.currencies[currency] 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. Internal API - used by Money parser.
68 69 70 |
# File 'lib/minting/mint/registry.rb', line 68 def self.currency_symbols CurrencyStore.currency_symbols end |
.money(amount, currency_code) ⇒ Money
Creates a new Money instance with the given amount and currency code.
11 12 13 14 15 16 |
# File 'lib/minting/mint/registry.rb', line 11 def self.money(amount, currency_code) currency = currency(currency_code) return Money.create(amount, currency) if currency raise ArgumentError, "[#{currency.inspect}] is not a registered currency." end |
.register_currency(code:, subunit: 0, symbol: '', priority: 0) ⇒ Currency
Registers a new currency if not already registered.
35 36 37 |
# File 'lib/minting/mint/registry.rb', line 35 def self.register_currency(code:, subunit: 0, symbol: '', priority: 0) CurrencyStore.currencies[code] || register_currency!(code:, subunit:, symbol:, priority:) end |
.register_currency!(code:, subunit:, symbol: '', priority: 0) ⇒ Currency
Strictly registers a new currency, raising a KeyError if already registered.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/minting/mint/registry.rb', line 48 def self.register_currency!(code:, subunit:, 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 only letters or '_' ('USD',, 'MY_COIN')" end currencies = CurrencyStore.currencies raise KeyError, "Currency: #{code} already registered" if currencies[code] currency = currencies[code] = Currency.new(code:, subunit:, symbol:, priority:) CurrencyStore.invalidate_symbols_cache currency end |