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



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

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

#divisions(russian_case) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/to_russian_words/utils.rb', line 79

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

#get_division(russian_case, counter, remaining) ⇒ Object



109
110
111
112
113
# File 'lib/to_russian_words/utils.rb', line 109

def get_division(russian_case, counter, remaining)
  # binding.pry
  variants = divisions(russian_case)[counter]
  pluralize(remaining, *variants)
end

#get_label(russian_case:, remaining:, hundred: 0, counter: 0) ⇒ Object



72
73
74
75
76
77
# File 'lib/to_russian_words/utils.rb', line 72

def get_label(russian_case:, remaining:, hundred: 0, counter: 0)
  female, male = under_hundred(russian_case, hundred, counter)
  res = female[remaining]
  res = male[remaining] if res.nil?
  res
end

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



22
23
24
25
26
27
28
29
30
# File 'lib/to_russian_words/utils.rb', line 22

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

#hundred_name(hundred, russian_case) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/to_russian_words/utils.rb', line 88

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

#numerical?(num) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#pluralize(number, singular, few, plural) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/to_russian_words/utils.rb', line 115

def pluralize(number, singular, few, plural)
  number = number.abs
  return plural if (11..14).include?(number % 100)

  case number % 10
  when 1 then singular
  when 2..4 then few
  else plural
  end
end

#result_below_one_thousand(num, counter, russian_case) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/to_russian_words/utils.rb', line 12

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

  if hundred == 0 && remaining != 0
    get_label(russian_case: russian_case, hundred: counter == 1 ? num : nil, counter: counter,
              remaining: remaining)
  end
end

#under_hundred(russian_case, hundred = 0, counter = 0) ⇒ Object



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
# File 'lib/to_russian_words/utils.rb', line 42

def under_hundred(russian_case, hundred = 0, counter = 0)
  case russian_case
  when "dative"
    if [1, 2].include?(hundred)
      [DATIVE_RUSSIAN_FEMALE_LABEL, DATIVE_UNDER_HUNDRED]
    else
      (
            if counter == 1
              [DATIVE_RUSSIAN_FEMALE_LABEL, DATIVE_UNDER_HUNDRED]
            else
              [DATIVE_UNDER_HUNDRED, DATIVE_UNDER_HUNDRED]
            end
          )
    end

  else
    if [1, 2].include?(hundred)
      [NOMINATIVE_RUSSIAN_FEMALE_LABEL, NOMINATIVE_UNDER_HUNDRED]
    else
      (
            if counter == 1
              [NOMINATIVE_RUSSIAN_FEMALE_LABEL, NOMINATIVE_UNDER_HUNDRED]
            else
              [NOMINATIVE_UNDER_HUNDRED, NOMINATIVE_UNDER_HUNDRED]
            end
          )
    end
  end
end