Class: Mint::Currency
- Inherits:
-
Data
- Object
- Data
- Mint::Currency
- Defined in:
- lib/minting/currency/currency.rb
Overview
Represents a specific currency unit, identified by ISO 4217 alphabetic code. Currency objects are immutable and define the properties of a monetary unit including its subunit precision, display symbol, and formatting rules.
Instance Attribute Summary collapse
-
#code ⇒ String
readonly
ISO 4217 currency code (e.g., “USD”, “EUR”).
-
#country ⇒ String?
readonly
Associated country code.
-
#fractional_multiplier ⇒ Integer
readonly
10^subunit, used for fractional conversions.
-
#name ⇒ String?
readonly
Currency name.
-
#priority ⇒ Integer
readonly
Parser precedence for symbol detection.
-
#subunit ⇒ Integer
readonly
Number of decimal places (0 for JPY, 2 for USD, 3 for IQD).
-
#symbol ⇒ String
readonly
Display symbol (e.g., “$”, “€”, “R$”).
Class Method Summary collapse
-
.for_code(code) ⇒ Currency?
Looks up a registered currency by its alpha code.
-
.for_symbol(symbol) ⇒ Currency?
Looks up a currency by its display symbol.
-
.register(code:, subunit: 0, symbol: '', priority: 0) ⇒ Currency
Registers a new currency, raising a KeyError if already registered.
-
.resolve(object) ⇒ Currency?
Resolves an object into a Currency, returning
nilwhen it can’t. -
.resolve!(object) ⇒ Currency
Resolves an object into a Currency, raising on failure.
-
.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
-
#initialize(code:, symbol:, subunit: 0, priority: 0, country: nil, name: nil) ⇒ Currency
constructor
A new instance of Currency.
-
#inspect ⇒ String
Debug representation.
-
#minimum_amount ⇒ Rational
Smallest representable amount (1/fractional_multiplier).
-
#normalize_amount(amount) ⇒ Object
Normalizes numeric amounts for this currency 1.
- #zero ⇒ Object
Constructor Details
#initialize(code:, symbol:, subunit: 0, priority: 0, country: nil, name: nil) ⇒ Currency
Returns a new instance of Currency.
26 27 28 29 30 31 32 |
# File 'lib/minting/currency/currency.rb', line 26 def initialize(code:, symbol:, subunit: 0, priority: 0, country: nil, name: nil) subunit = subunit.to_i priority = priority.to_i fractional_multiplier = 10**subunit super(code:, subunit:, symbol:, priority:, country:, name:, fractional_multiplier:) end |
Instance Attribute Details
#code ⇒ String (readonly)
ISO 4217 currency code (e.g., “USD”, “EUR”)
18 19 20 |
# File 'lib/minting/currency/currency.rb', line 18 def code @code end |
#country ⇒ String? (readonly)
Associated country code
18 19 20 |
# File 'lib/minting/currency/currency.rb', line 18 def country @country end |
#fractional_multiplier ⇒ Integer (readonly)
10^subunit, used for fractional conversions
18 19 20 |
# File 'lib/minting/currency/currency.rb', line 18 def fractional_multiplier @fractional_multiplier end |
#name ⇒ String? (readonly)
Currency name
18 19 20 |
# File 'lib/minting/currency/currency.rb', line 18 def name @name end |
#priority ⇒ Integer (readonly)
Parser precedence for symbol detection
18 19 20 |
# File 'lib/minting/currency/currency.rb', line 18 def priority @priority end |
#subunit ⇒ Integer (readonly)
Number of decimal places (0 for JPY, 2 for USD, 3 for IQD)
18 19 20 |
# File 'lib/minting/currency/currency.rb', line 18 def subunit @subunit end |
#symbol ⇒ String (readonly)
Display symbol (e.g., “$”, “€”, “R$”)
18 19 20 |
# File 'lib/minting/currency/currency.rb', line 18 def symbol @symbol end |
Class Method Details
.for_code(code) ⇒ Currency?
Looks up a registered currency by its alpha code.
95 96 97 |
# File 'lib/minting/currency/currency.rb', line 95 def Currency.for_code(code) Registry.currencies[code] end |
.for_symbol(symbol) ⇒ Currency?
Looks up a currency by its display symbol.
103 104 105 |
# File 'lib/minting/currency/currency.rb', line 103 def Currency.for_symbol(symbol) Registry.currency_for_symbol(symbol) end |
.register(code:, subunit: 0, symbol: '', priority: 0) ⇒ Currency
Registers a new currency, raising a KeyError if already registered.
57 58 59 |
# File 'lib/minting/currency/currency.rb', line 57 def Currency.register(code:, subunit: 0, symbol: '', priority: 0) Registry.register(code:, subunit:, symbol:, priority:) end |
.resolve(object) ⇒ Currency?
Resolves an object into a Mint::Currency, returning nil when it can’t.
Accepts nil, String, Mint::Currency, or Money. Passing a Money extracts its currency
70 71 72 73 74 75 76 77 78 |
# File 'lib/minting/currency/currency.rb', line 70 def Currency.resolve(object) case object when NilClass then nil when Currency then object when Money then object.currency when String then Currency.for_code object else raise ArgumentError, "currency must be [Currency], [Money], [String] or nil (#{object})" end end |
.resolve!(object) ⇒ Currency
Resolves an object into a Mint::Currency, raising on failure.
Like resolve but raises when the result would be nil.
87 88 89 |
# File 'lib/minting/currency/currency.rb', line 87 def Currency.resolve!(object) resolve(object) or raise ArgumentError, "Could not resolve (#{object}) into a currency" end |
Instance Method Details
#inspect ⇒ String
Returns debug representation.
35 |
# File 'lib/minting/currency/currency.rb', line 35 def inspect = "<Currency:(#{code} #{symbol} #{subunit} #{name})>" |
#minimum_amount ⇒ Rational
Returns smallest representable amount (1/fractional_multiplier).
38 |
# File 'lib/minting/currency/currency.rb', line 38 def minimum_amount = Rational(1, fractional_multiplier) |