Module: NumbersAndWordsPl::Helper

Defined in:
lib/numbers_and_words_pl/helper.rb

Constant Summary collapse

PL_NUMBERS =
YAML.parse(File.read(File.dirname(__FILE__) + '/pl.yml')).to_ruby['numbers']

Instance Method Summary collapse

Instance Method Details

#to_words_pl(integer) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/numbers_and_words_pl/helper.rb', line 5

def to_words_pl(integer)
  case integer
  when 0..9
    PL_NUMBERS['ones'][integer]
  when 10..19
    PL_NUMBERS['teens'][integer - 10]
  when 20..99
    tens, ones = integer.to_s.split('').map(&:to_i)
    "#{PL_NUMBERS['tens'][tens]} #{PL_NUMBERS['ones'][ones] if ones > 0}".strip
  when 100..999
    hundreds = integer.to_s[0].to_i - 1
    rest = integer.to_s[1..-1].to_i
    "#{PL_NUMBERS['hundreds'][hundreds]} #{to_words_pl rest if rest > 0}".strip
  when 1_000..999_999
    thousands = integer.to_s[0..-4].to_i
    one_few_many = case thousands
                   when 1
                     'one'
                   when 2..4
                     'few'
                   else
                     'many'
                   end
    rest = integer.to_s[-3..-1].to_i
    "#{to_words_pl thousands} #{PL_NUMBERS['thousands'][one_few_many]} #{to_words_pl rest if rest > 0}".strip
  else
    raise "number #{integer} not supported"
  end
end