Module: Timeprice::Supported

Defined in:
lib/timeprice/supported.rb

Overview

Canonical lists of supported country and currency codes, plus the bidirectional currency↔country map used by ‘Compare` and CLI output.

Everything that needs to know “which currency pairs with which CPI series” must read it from here — duplicating the map elsewhere has bitten us before when a new country was added in one place and forgotten in the other.

Constant Summary collapse

COUNTRIES =
%w[US UK EU JP VN].freeze
CURRENCIES =
%w[USD GBP EUR JPY VND].freeze
COUNTRY_TO_CURRENCY =
{
  "US" => "USD",
  "UK" => "GBP",
  "EU" => "EUR",
  "JP" => "JPY",
  "VN" => "VND",
}.freeze
CURRENCY_TO_COUNTRY =
COUNTRY_TO_CURRENCY.invert.freeze
ZERO_DECIMAL_CURRENCIES =

Currencies with no minor unit — formatted as whole numbers.

%w[JPY VND].freeze

Class Method Summary collapse

Class Method Details

.country?(country) ⇒ Boolean

Parameters:

  • country (String)

Returns:

  • (Boolean)


40
41
42
# File 'lib/timeprice/supported.rb', line 40

def country?(country)
  COUNTRIES.include?(country.to_s.upcase)
end

.country_for_currency(currency) ⇒ String?

Returns country code, or nil if unsupported.

Parameters:

  • currency (String)

    ISO 4217 code (e.g. “USD”)

Returns:

  • (String, nil)

    country code, or nil if unsupported



52
53
54
# File 'lib/timeprice/supported.rb', line 52

def country_for_currency(currency)
  CURRENCY_TO_COUNTRY[currency.to_s.upcase]
end

.currency?(currency) ⇒ Boolean

Parameters:

  • currency (String)

Returns:

  • (Boolean)


46
47
48
# File 'lib/timeprice/supported.rb', line 46

def currency?(currency)
  CURRENCIES.include?(currency.to_s.upcase)
end

.currency_for_country(country) ⇒ String?

Returns currency code, or nil if unsupported.

Parameters:

  • country (String)

    country code (e.g. “US”)

Returns:

  • (String, nil)

    currency code, or nil if unsupported



58
59
60
# File 'lib/timeprice/supported.rb', line 58

def currency_for_country(country)
  COUNTRY_TO_CURRENCY[country.to_s.upcase]
end

.decimals_for(currency) ⇒ Integer

ISO 4217 minor-unit count for a currency. Falls back to 2 for unknown codes so callers can still render some value rather than crashing.

Parameters:

  • currency (String)

Returns:

  • (Integer)


34
35
36
# File 'lib/timeprice/supported.rb', line 34

def decimals_for(currency)
  ZERO_DECIMAL_CURRENCIES.include?(currency.to_s.upcase) ? 0 : 2
end