Class: Mint::Money::Formatter Private

Inherits:
Object
  • Object
show all
Extended by:
FormatterValidator
Defined in:
lib/minting/money/format/formatter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Compiles and caches formatter lambdas for a fixed combination of format template, currency, and separator configuration.

Use Formatter.for to obtain a cached instance; avoid new directly unless you want an uncached formatter (typically only useful for testing).

Constant Summary collapse

SUBUNIT_PLACEHOLDER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"\uE000"
THOUSAND_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Matches a digit followed by groups of exactly 3 digits that terminate at a non-digit or end-of-string. Used to insert thousand separators. e.g. "1234567" → "1" matches before "234" + "567" at string end.

/(\d)(?=(?:\d{3})+(?:[^\d]|$))/

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FormatterValidator

validate_format!, validate_separators!

Constructor Details

#initialize(format, decimal, thousand) ⇒ Formatter

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 a new instance of Formatter.



32
33
34
35
36
37
# File 'lib/minting/money/format/formatter.rb', line 32

def initialize(format, decimal, thousand)
  @format = format
  @decimal = decimal
  @thousand = thousand
  compile
end

Class Method Details

.cacheObject

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.



15
# File 'lib/minting/money/format/formatter.rb', line 15

def self.cache = @cache ||= {}

.for(format, decimal, thousand) ⇒ Object

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 a cached Mint::Money::Formatter for the given configuration.

Parameters:

  • format (Hash{Symbol => String})

    per-sign templates

  • decimal (String)

    decimal separator

  • thousand (String, false)

    thousands delimiter (+false+ disables)



21
22
23
24
25
26
27
28
29
30
# File 'lib/minting/money/format/formatter.rb', line 21

def self.for(format, decimal, thousand)
  key = [format, decimal, thousand]
  formatter = cache[key]
  return formatter if formatter

  validate_format!(format)
  validate_separators!(decimal:, thousand:)

  cache[key] = new(format, decimal, thousand)
end

Instance Method Details

#format(money) ⇒ Object

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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/minting/money/format/formatter.rb', line 45

def format(money)
  amount = money.amount
  currency = money.currency

  templates = @has_placeholder ? @templates_by_subunit[currency.subunit] : @templates

  template = templates[amount <=> 0] || templates[1]

  display_amount = @has_negative_template && amount < 0 ? -amount : amount
  integral = display_amount.to_i

  result = Kernel.format(template,
                         currency: currency.code,
                         dsymbol: @needs_dsymbol && currency.dsymbol,
                         symbol: currency.symbol,
                         amount: display_amount,
                         integral: integral,
                         fractional: @needs_fractional ? money.fractional.abs : 0)
  apply_separators(result, integral)
end