Module: ToRussianWords::Utils

Includes:
Divisions, RussianGenderLabels, UnderHundred
Included in:
ToRussianWords
Defined in:
lib/to_russian_words/utils.rb

Constant Summary

Constants included from RussianGenderLabels

RussianGenderLabels::DATIVE_RUSSIAN_FEMALE_LABEL, RussianGenderLabels::NOMINATIVE_RUSSIAN_FEMALE_LABEL

Constants included from Divisions

Divisions::DATIVE_DIVISIONS, Divisions::NOMINATIVE_DIVISIONS

Constants included from UnderHundred

ToRussianWords::UnderHundred::DATIVE_UNDER_HUNDRED, ToRussianWords::UnderHundred::NOMINATIVE_UNDER_HUNDRED

Instance Method Summary collapse

Instance Method Details

#check_sign(num) ⇒ Object



26
27
28
# File 'lib/to_russian_words/utils.rb', line 26

def check_sign(num)
  num < 0 ? [num.abs, 'negative '] : [num, '']
end

#divisions(russian_case) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/to_russian_words/utils.rb', line 45

def divisions(russian_case)
  case russian_case
  when 'dative'
    DATIVE_DIVISIONS
  else
    NOMINATIVE_DIVISIONS
  end
end

#higher_than_hundred(hundred, remaining, counter, russian_case) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/to_russian_words/utils.rb', line 18

def higher_than_hundred(hundred, remaining, counter, russian_case)
  century = (hundred == 1 ? '' : under_hundred(russian_case, hundred)[hundred])
  if remaining != 0
    return century + "#{hundred_name(hundred, russian_case)} " + under_hundred(russian_case)[remaining]
  end
  return century + "#{hundred_name(hundred, russian_case)} " if remaining == 0
end

#hundred_name(hundred, russian_case) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/to_russian_words/utils.rb', line 54

def hundred_name(hundred, russian_case)
  case russian_case
  when 'dative'
    if hundred == 1
      'ста'
    else
      'сот'
    end
  else
    if [1, 4].include? hundred
      'сто'
    elsif hundred == 2
      'сти'
    elsif hundred == 3
      'ста'
    else
      'сот'
    end
  end
end

#numerical?(num) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/to_russian_words/utils.rb', line 30

def numerical?(num)
  Integer(num)
rescue
  raise 'A whole number is expected'
end

#result_below_one_thousand(num, counter, russian_case) ⇒ Object



11
12
13
14
15
16
# File 'lib/to_russian_words/utils.rb', line 11

def result_below_one_thousand(num, counter, russian_case)
  hundred, remaining = num.divmod(100)

  return higher_than_hundred(hundred, remaining, counter, russian_case) if hundred != 0
  under_hundred(russian_case, counter == 1 ? num : nil)[remaining] if hundred == 0 && remaining != 0
end

#under_hundred(russian_case, hundred = 0) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/to_russian_words/utils.rb', line 36

def under_hundred(russian_case, hundred = 0)
  case russian_case
  when 'dative'
    [1, 2].include?(hundred) ? DATIVE_RUSSIAN_FEMALE_LABEL : DATIVE_UNDER_HUNDRED
  else
    [1, 2].include?(hundred) ? NOMINATIVE_RUSSIAN_FEMALE_LABEL : NOMINATIVE_UNDER_HUNDRED
  end
end