Module: Mint
- Extended by:
- Mint
- Included in:
- Mint
- Defined in:
- lib/minting/mint/mint.rb,
lib/minting/money/clamp.rb,
lib/minting/money/money.rb,
lib/minting/mint/dsl/range.rb,
lib/minting/money/coercion.rb,
lib/minting/mint/dsl/string.rb,
lib/minting/mint/dsl/numeric.rb,
lib/minting/money/comparable.rb,
lib/minting/money/conversion.rb,
lib/minting/currency/currency.rb,
lib/minting/money/format/to_s.rb,
lib/minting/mint/dsl/top_level.rb,
lib/minting/mint/parser/parser.rb,
lib/minting/money/constructors.rb,
lib/minting/mint/locale_backend.rb,
lib/minting/mint/registry/zeros.rb,
lib/minting/mint/registry/symbols.rb,
lib/minting/mint/parser/separators.rb,
lib/minting/mint/registry/registry.rb,
lib/minting/money/allocation/split.rb,
lib/minting/money/format/formatting.rb,
lib/minting/money/arithmetics/methods.rb,
lib/minting/mint/registry/registration.rb,
lib/minting/money/allocation/allocation.rb,
lib/minting/money/arithmetics/operators.rb
Overview
Mint registry: manages all cached state
Defined Under Namespace
Modules: RangeStepPatch, Registry Classes: Currency, Money, UnknownCurrency
Class Attribute Summary collapse
-
.locale_backend ⇒ Proc, ...
Optional callable that returns a Hash with locale-aware formatting defaults.
Class Method Summary collapse
-
.currency_for_code(code) ⇒ Currency?
Looks up a registered currency by its alpha code.
-
.currency_for_symbol(symbol) ⇒ Currency?
Looks up a currency by its display symbol.
-
.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, raising a KeyError if already registered.
-
.use_top_level_constants! ⇒ Object
Registers top-level ::Money and ::Currency constants as aliases for Mint’s classes.
-
.world_currencies ⇒ Hash{String => Currency}
private
The frozen world-currencies hash.
-
.zero(currency) ⇒ Money
Returns a zero Money in the given currency, useful as a default value for discounts, totals, or placeholders.
Instance Method Summary collapse
-
#parse(input, currency = nil) ⇒ Money
Parses a human-readable money string into a Money object.
Class Attribute Details
.locale_backend ⇒ Proc, ...
Optional callable that returns a Hash with locale-aware formatting defaults.
The callable receives no arguments and returns a Hash with these keys:
[+:decimal+] Decimal separator (e.g. +","+)
[+:thousand+] Thousands delimiter (e.g. +"."+)
[+:format+] Format template string (e.g. +"%<amount>f %<symbol>s"+)
When set, #to_s and #format use these values as fallbacks when the corresponding parameter is not explicitly provided.
27 28 29 |
# File 'lib/minting/mint/locale_backend.rb', line 27 def locale_backend @locale_backend end |
Class Method Details
.currency_for_code(code) ⇒ Currency?
Looks up a registered currency by its alpha code.
Unlike currency, this performs a direct hash lookup and only accepts strings.
27 28 29 |
# File 'lib/minting/mint/mint.rb', line 27 def self.currency_for_code(code) Registry.currencies[code] end |
.currency_for_symbol(symbol) ⇒ Currency?
Looks up a currency by its display symbol.
35 36 37 |
# File 'lib/minting/mint/mint.rb', line 35 def self.currency_for_symbol(symbol) Registry.currency_for_symbol(symbol) end |
.money(amount, currency_code) ⇒ Money
Creates a new Money instance with the given amount and currency code.
15 |
# File 'lib/minting/mint/mint.rb', line 15 def self.money(amount, currency_code) = Money.create(amount, currency_code) |
.register_currency(code:, subunit: 0, symbol: '', priority: 0) ⇒ Currency
Registers a new currency, raising a KeyError if already registered.
56 57 58 |
# File 'lib/minting/mint/mint.rb', line 56 def self.register_currency(code:, subunit: 0, symbol: '', priority: 0) Registry.register(code:, subunit:, symbol:, priority:) end |
.use_top_level_constants! ⇒ Object
Registers top-level ::Money and ::Currency constants as aliases for Mint’s classes.
8 9 10 11 12 13 14 15 16 |
# File 'lib/minting/mint/dsl/top_level.rb', line 8 def self.use_top_level_constants! if !defined?(::Money) && !defined?(::Currency) require 'minting/mint/aliases' elsif ::Money == Mint::Money && ::Currency == Mint::Currency warn 'Warning: Money and Currency already defined as Mint aliases, skipping' else raise NameError, 'Cannot define top-level Money or Currency constants: already defined' 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.
Returns the frozen world-currencies hash.
19 |
# File 'lib/minting/mint/mint.rb', line 19 def self.world_currencies = Registry.world_currencies |
Instance Method Details
#parse(input, currency = nil) ⇒ Money
Parses a human-readable money string into a Money object.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/minting/mint/parser/parser.rb', line 22 def parse(input, currency = nil) raise ArgumentError, 'input must be a String' unless input.is_a?(String) input = input.strip raise ArgumentError, 'input cannot be empty' if input.empty? currency = Currency.resolve(currency) || parse_currency(input) raise ArgumentError, "Currency [#{currency}] not registered" unless currency amount = currency.normalize_amount(parse_amount(input)) Mint::Money.new(amount, currency) end |