Class: Num2words::Converter

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

Class Method Summary collapse

Class Method Details

.to_currency(amount, *args, **opts) ⇒ Object



41
42
43
44
45
46
47
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
# File 'lib/num2words/converter.rb', line 41

def to_currency(amount, *args, **opts)
  locale = args.first.is_a?(Symbol) ? args.first : opts[:locale] || I18n.default_locale
  word_case = opts[:word_case] || :downcase
  currency = (opts[:code] || Num2words.default_currency(locale)).to_s.upcase.to_sym
  minor = opts[:minor] || :always

  validate_option!(:minor, minor, %i[always nonzero never])

  unless Num2words.available_currencies(locale).include?(currency)
    warn I18n.t("num2words.warnings.currency_not_available",
                currency: currency, locale: locale) if Num2words.currency_warnings
    currency = Num2words.default_currency(locale)
  end

  currency_data = I18n.t("num2words.currencies.#{currency}", locale: locale) or
    raise ArgumentError, "Currency #{currency} not defined in locale #{locale}"

  decimal_amount = decimal_currency_amount(amount)
  major_value, minor_value = format('%.2f', decimal_amount.abs).split('.').map(&:to_i)

  parts = [
    to_words(major_value, locale: locale),
    pluralize(major_value, *currency_data[:major_unit])
  ]

  include_minor = minor == :always || (minor == :nonzero && minor_value.positive?)
  if include_minor
    parts.concat([
      to_words(minor_value, locale: locale, feminine: true),
      pluralize(minor_value, *currency_data[:minor_unit])
    ])
  end

  parts.unshift(Locales[locale]::GRAMMAR[:minus] || "minus") if decimal_amount.negative?

  apply_case(parts.join(" ").strip, word_case)
end

.to_words(number, *args, **opts) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/num2words/converter.rb', line 10

def to_words(number, *args, **opts)
  locale = args[0].is_a?(Symbol) ? args[0] : opts[:locale] || I18n.default_locale
  type_only = args[1].is_a?(Symbol) ? args[1] : opts[:only]
  type_short = args[2].is_a?(TrueClass) || args[2].is_a?(FalseClass) ? args[2] : opts[:short] || false

  feminine = opts[:feminine] || false
  style = opts[:style] || :fraction
  word_case = opts[:word_case] || :default
  date_format = opts[:format] || :default
  date_case = opts[:date_case] || :default
  joiner = opts[:joiner] || :default

  validate_option!(:date_case, date_case, %i[default genitive])
  validate_option!(:joiner, joiner, %i[default and])

  locale_data = Locales[locale]

  result = case detect_type(number)
           when :float then to_words_fractional(number, locale, feminine, locale_data, style: style, joiner: joiner)
           when :integer then to_words_integer(number, locale, feminine, locale_data)
           when :datetime then to_words_datetime(number, locale, locale_data, format: date_format, only: type_only, short: type_short, date_case: date_case)
           when :date then to_words_date(number, locale, locale_data, format: date_format, date_case: date_case)
           when :time then to_words_time(number, locale, locale_data, short: type_short)
           else nil
           end

  raise ArgumentError, "Unsupported input type: #{number.inspect}" if result.nil?

  apply_case(result, word_case)
end