Class: Num2words::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/num2words/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



7
8
9
# File 'lib/num2words/config.rb', line 7

def initialize
  reset!
end

Instance Attribute Details

#currency_warningsObject

Returns the value of attribute currency_warnings.



5
6
7
# File 'lib/num2words/config.rb', line 5

def currency_warnings
  @currency_warnings
end

Instance Method Details

#available_currencies(locale = I18n.locale) ⇒ Object



26
27
28
# File 'lib/num2words/config.rb', line 26

def available_currencies(locale = I18n.locale)
  I18n.t("num2words.currencies", locale: locale).keys.map(&:to_sym)
end

#available_localesObject



62
63
64
65
66
# File 'lib/num2words/config.rb', line 62

def available_locales
  @available_locales ||= Dir[File.expand_path("../../config/locales/*.yml", __dir__)]
                         .map { |file| File.basename(file, ".yml").to_sym }
                         .sort
end

#currency_available?(locale, currency) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/num2words/config.rb', line 30

def currency_available?(locale, currency)
  available_currencies(locale).include?(currency.to_s.upcase.to_sym)
end

#currency_info(locale, currency) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/num2words/config.rb', line 34

def currency_info(locale, currency)
  code = currency.to_s.upcase.to_sym
  data = I18n.t("num2words.currencies", locale: locale)[code]

  return unless data

  {
    code: code,
    major_unit: data[:major_unit],
    minor_unit: data[:minor_unit],
    symbol: data[:symbol]
  }
end

#currency_options(locale = I18n.locale, format: :symbol) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/num2words/config.rb', line 52

def currency_options(locale = I18n.locale, format: :symbol)
  validate_currency_options_format!(format)

  available_currencies(locale).map do |code|
    info = currency_info(locale, code)

    [currency_option_label(info, format), code]
  end
end

#default_currency(locale = nil, currency = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/num2words/config.rb', line 11

def default_currency(locale = nil, currency = nil)
  if currency
    currency = currency.to_s.upcase.to_sym
    available = available_currencies(locale)

    if available.include?(currency)
      locale ? @local_currency[locale] = currency : @global_currency = currency
    elsif currency_warnings
      warn I18n.t("num2words.warnings.currency_not_available", lang: locale, currency: currency, locale: locale)
    end
  end

  locale ? (@local_currency[locale] || available_currencies(locale).first || @global_currency) : @global_currency
end

#default_currency_info(locale = I18n.locale) ⇒ Object



48
49
50
# File 'lib/num2words/config.rb', line 48

def default_currency_info(locale = I18n.locale)
  currency_info(locale, default_currency(locale))
end

#locale_options(format: :code) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/num2words/config.rb', line 68

def locale_options(format: :code)
  validate_locale_options_format!(format)

  available_locales.map do |locale|
    [locale_option_label(locale, format), locale]
  end
end

#reset!(locale = nil) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/num2words/config.rb', line 76

def reset!(locale = nil)
  return @local_currency.delete(locale) if locale

  @global_currency   = available_currencies.first
  @local_currency    = {}
  @currency_warnings = true
end