Class: Worldwide::Currency

Inherits:
Object
  • Object
show all
Includes:
PluralizationHelper
Defined in:
lib/worldwide/currency.rb

Constant Summary collapse

EXCEPTIONS =
{ HKD: "HK$" }
SUPPORTED_ALTERNATE_FORMATS =
[:japan]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PluralizationHelper

#translate_plural

Constructor Details

#initialize(code:) ⇒ Currency

Returns a new instance of Currency.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/worldwide/currency.rb', line 29

def initialize(code:)
  adjusted_code = code.to_s.upcase.rjust(3, "0")
  if adjusted_code.match?(/[A-Z][A-Z][A-Z]/)
    @currency_code = adjusted_code
    @numeric_code = Currencies.numeric_code_for(adjusted_code)
  elsif adjusted_code.match?(/[0-9]+/)
    @currency_code = Currencies.alpha_code_for(adjusted_code)
    @numeric_code = adjusted_code
  else
    raise ArgumentError, "Currency code must be an ISO-4217 code, either alpha-three or numeric-three"
  end
end

Instance Attribute Details

#currency_codeObject (readonly)

Returns the value of attribute currency_code.



27
28
29
# File 'lib/worldwide/currency.rb', line 27

def currency_code
  @currency_code
end

#numeric_codeObject (readonly)

Returns the value of attribute numeric_code.



27
28
29
# File 'lib/worldwide/currency.rb', line 27

def numeric_code
  @numeric_code
end

Class Method Details

.digits_and_roundingObject



14
15
16
17
18
# File 'lib/worldwide/currency.rb', line 14

def digits_and_rounding
  @digits_and_rounding ||= YAML.safe_load(
    File.read(File.join(Worldwide::Paths::OTHER_DATA_ROOT, "currency", "digits.yml")),
  ).freeze
end

.minor_symbolsObject



20
21
22
23
24
# File 'lib/worldwide/currency.rb', line 20

def minor_symbols
  @minor_symbols ||= YAML.safe_load(
    File.read(File.join(Worldwide::Paths::OTHER_DATA_ROOT, "currency", "minor_symbols.yml")),
  ).freeze
end

Instance Method Details

#decimalsObject

Returns the number of decimal places needed to represent the currency’s minor unit



43
44
45
# File 'lib/worldwide/currency.rb', line 43

def decimals
  Currency.digits_and_rounding.dig(@currency_code, "digits") || 2
end

#format_explicit(amount, as_minor_units: false, decimal_places: nil, humanize: nil, locale: I18n.locale, use_symbol: true) ⇒ Object

returns the amount formatted using the explicit currency code in the given locale



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/worldwide/currency.rb', line 93

def format_explicit(
  amount,
  as_minor_units: false,
  decimal_places: nil,
  humanize: nil,
  locale: I18n.locale,
  use_symbol: true
)
  short_version = format_short(
    amount,
    as_minor_units: as_minor_units,
    decimal_places: decimal_places,
    humanize: humanize,
    locale: locale,
    use_symbol: use_symbol,
  )
  if short_version.include?(@currency_code)
    short_version
  else
    "#{short_version} #{@currency_code}"
  end
end

#format_short(amount, as_minor_units: false, decimal_places: nil, humanize: nil, locale: I18n.locale, use_symbol: true) ⇒ Object

returns the amount formatted using the short-form symbol in the given locale



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/worldwide/currency.rb', line 48

def format_short(
  amount,
  as_minor_units: false,
  decimal_places: nil,
  humanize: nil,
  locale: I18n.locale,
  use_symbol: true
)
  decimal_places = humanize ? nil : decimals if decimal_places.nil?
  use_symbol = true if as_minor_units

  if SUPPORTED_ALTERNATE_FORMATS.include?(humanize)
    return format_alternate(
      amount,
      decimal_places: decimal_places,
      humanize: humanize,
      locale: locale,
      use_symbol: use_symbol,
    )
  end

  if as_minor_units && has_minor_symbol?
    return format_minor_units(
      amount,
      decimal_places: decimal_places,
      humanize: humanize,
      locale: locale,
    )
  end

  if use_symbol
    currency_symbol = symbol(locale: locale) || @currency_code
    combine(
      amount: amount,
      decimal_places: decimal_places,
      humanize: humanize,
      locale: locale,
      symbol: currency_symbol,
    )
  else
    formatted_amount(amount, decimal_places: decimal_places, humanize: humanize, locale: locale)
  end
end

#label(count: 1) ⇒ Object



144
145
146
147
148
# File 'lib/worldwide/currency.rb', line 144

def label(count: 1)
  format_currency_text(translate_plural("currencies.#{@currency_code}", count: count))
rescue I18n::InvalidPluralizationData
  name
end

#nameObject



150
151
152
# File 'lib/worldwide/currency.rb', line 150

def name
  format_currency_text(Worldwide::Cldr.t("currencies.#{@currency_code}.name"))
end

#symbol(locale: I18n.locale) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/worldwide/currency.rb', line 116

def symbol(locale: I18n.locale)
  raw_symbol = Worldwide::Cldr.t("currencies.#{@currency_code}.narrow_symbol", default: nil, locale: locale) ||
    Worldwide::Cldr.t("currencies.#{@currency_code}.symbol", default: nil, locale: locale)

  return nil if raw_symbol.nil?

  # For some locales (e.g., HK), in-market folks have requested that we leave the CLDR behaviour untouched
  exceptional_symbol = EXCEPTIONS.fetch(@currency_code.to_sym, nil)
  return exceptional_symbol if exceptional_symbol

  # For some locales, CLDR defines the "symbol" to be the ISO currency code.
  # In those cases, we want to leave the raw_symbol unaltered.
  return raw_symbol if raw_symbol == @currency_code.to_s

  # For some currencies in some locales, CLDR includes alphabetic characters along with the symbol.
  # For example, CLDR defines the "symbol" as "$US" for :USD in :fr, and as "CA$" for :CAD in :'en'.
  # So, let's see what we have left after stripping the country code, if present, from the symbol.

  country_code = @currency_code.to_s[0..1]
  bare_symbol = raw_symbol.gsub(country_code, "")

  if bare_symbol.empty? || bare_symbol == "."
    raw_symbol
  else
    bare_symbol
  end
end