Module: Mint

Defined in:
lib/minting/mint/i18n.rb,
lib/minting/mint/mint.rb,
lib/minting/money/clamp.rb,
lib/minting/money/money.rb,
lib/minting/money/parse.rb,
lib/minting/mint/dsl/range.rb,
lib/minting/money/coercion.rb,
lib/minting/money/rounding.rb,
lib/minting/money/comparable.rb,
lib/minting/money/conversion.rb,
lib/minting/currency/currency.rb,
lib/minting/currency/registry.rb,
lib/minting/currency/rounding.rb,
lib/minting/money/format/to_s.rb,
lib/minting/money/constructors.rb,
lib/minting/mint/registry/zeros.rb,
lib/minting/money/format/format.rb,
lib/minting/mint/registry/crypto.rb,
lib/minting/mint/registry/symbols.rb,
lib/minting/mint/registry/registry.rb,
lib/minting/money/allocation/split.rb,
lib/minting/money/format/formatter.rb,
lib/minting/money/format/validator.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

Constant Summary collapse

EMPTY_LOCALE =
{ decimal: nil, thousand: nil, format: nil }.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.locale_backendProc, ...

Optional callable that returns a Hash with locale-aware formatting defaults.

The callable receives the locale (as passed to +#format+'s locale: kwarg, or nil when omitted) and returns a Hash with these keys (strings and symbols are treated interchangeably):

[+decimal+]   Decimal separator (e.g. +","+) — also accepts +:separator+
[+thousand+]  Thousands delimiter (e.g. +"."+) — also accepts +:delimiter+
[+format+]    Format template string (e.g. +"%<amount>f %<symbol>s"+)

When set, #to_formatted_s and #format use these values as fallbacks when the corresponding parameter is not explicitly provided.

Examples:

Per-locale formatting with string keys

LOCALE_DATA = {
  'en'    => { decimal: '.', thousand: ',', format: '%<symbol>s%<amount>f' },
  'de'    => { decimal: ',', thousand: '.', format: '%<amount>f %<currency>s' },
}.freeze
Mint.locale_backend = ->(locale) { LOCALE_DATA[locale.to_s] || {} }

Rails I18n integration (direct pass-through, no mapping needed)

Mint.locale_backend = ->(locale = nil) {
  I18n.with_locale(locale || I18n.default_locale) do
    I18n.t('number.currency.format', default: {})
  end
}

Returns:

  • (Proc, #call, nil)


33
34
35
# File 'lib/minting/mint/i18n.rb', line 33

def locale_backend
  @locale_backend
end

Class Method Details

.fetch_locale_key(hash, key) ⇒ String?

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 locale key from a hash, trying both symbol and string forms.

Supports aliases: :decimal checks :decimal and :separator, :thousand checks :thousand and :delimiter, :format checks :format.

Parameters:

  • hash (Hash)

    locale config hash

  • key (Symbol)

    the primary key (+:decimal+, :thousand, or :format)

Returns:

  • (String, nil)

    the value found, or nil



66
67
68
69
70
71
72
73
# File 'lib/minting/mint/i18n.rb', line 66

def self.fetch_locale_key(hash, key)
  aliases = { decimal: %i[decimal separator], thousand: %i[thousand delimiter], format: [:format] }
  aliases.fetch(key).each do |name|
    val = hash[name] || hash[name.to_s]
    return val unless val.nil?
  end
  nil
end

.money(amount, currency_code) ⇒ Money

Creates a new Money instance with the given amount and currency code.

Parameters:

  • amount (Numeric)

    the financial value

  • currency_code (String, Currency, Money, nil)

    Currency code, object, Money whose currency to reuse, or nil. Passed through Mint::Currency.resolve! so all accepted types resolve to a registered currency.

Returns:

  • (Money)

    the instantiated Money object

Raises:

  • (ArgumentError)

    if the amount is not a Numeric

  • (Mint::UnknownCurrency)

    if the currency code is not registered. Mint::UnknownCurrency inherits from ArgumentError.



23
# File 'lib/minting/mint/mint.rb', line 23

def self.money(amount, currency_code) = Money.from(amount, currency_code)

.resolve_locale_backend(locale = nil) ⇒ Hash

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.

Resolves the locale backend configuration into a Hash.

Handles three backend types:

  • Hash: returned as-is
  • +Proc+/callable: called with the locale (or no args for 0-arity), result must be a Hash or nil
  • nil: returns empty Hash

Invalid return values or backends emit a warning and return {}.

Parameters:

  • locale (Symbol, String, nil) (defaults to: nil)

    locale passed to the backend callable

Returns:

  • (Hash)

    locale configuration (possibly empty)



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/minting/mint/i18n.rb', line 88

def self.resolve_locale_backend(locale = nil)
  case bk = Mint.locale_backend
  when Hash     then bk
  when NilClass then {}
  else
    if bk.respond_to?(:call)
      args = bk.respond_to?(:arity) && bk.arity == 0 ? [] : [locale]
      result = bk.call(*args)
      return result if result.is_a?(Hash) || result.nil?

      warn "ignoring invalid locale_backend result: #{result.inspect}"
      return {}
    end

    warn "ignoring invalid locale_backend: #{bk.inspect}"
    {}
  end
end

.resolve_locale_for(locale: nil) ⇒ Hash{Symbol => String, nil}

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.

Resolves locale-aware formatting defaults from Mint.locale_backend.

Returns a Hash with :decimal, :thousand, and :format keys (any of which may be nil if the backend doesn't provide them).

Parameters:

  • locale (Symbol, String, nil) (defaults to: nil)

    locale passed to the backend callable

Returns:

  • (Hash{Symbol => String, nil})


44
45
46
47
48
49
50
51
52
53
# File 'lib/minting/mint/i18n.rb', line 44

def self.resolve_locale_for(locale: nil)
  return EMPTY_LOCALE if Mint.locale_backend.nil?

  lc = resolve_locale_backend(locale)
  return EMPTY_LOCALE if lc.empty?

  { decimal: fetch_locale_key(lc, :decimal),
    thousand: fetch_locale_key(lc, :thousand),
    format: fetch_locale_key(lc, :format) }.freeze
end