Module: UaEnv::Pluralization

Defined in:
lib/pluralizer/pluralizer.rb

Overview

:nodoc:

Defined Under Namespace

Modules: FloatFormatting, NumericFormatting

Class Method Summary collapse

Class Method Details

.choose_plural(amount, *variants) ⇒ Object

Вибирає потрібний відмінок в залежності від числа



8
9
10
11
12
# File 'lib/pluralizer/pluralizer.rb', line 8

def self.choose_plural( amount, *variants )
  variant = ( (amount % 10 == 1 && amount % 100 != 11) ? ( 1 ) : ( amount % 10 >= 2 && amount % 10 <= 4 && (amount % 100 < 10 || amount % 100 >= 20) ) ? 2 : 3 )

  variants[variant - 1]
end

.grn(amount) ⇒ Object

self.choose_plural



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pluralizer/pluralizer.rb', line 14

def self.grn( amount )
  pts = []
  pts << UaEnv::Pluralization::sum_string( amount.to_i, 2, "гривня", "гривні", "гривень" ) unless amount.to_i == 0

  if amount.kind_of?( Float )
    remainder = ( amount.divmod(1)[1] * 100 ).round

    if ( remainder == 100 )
      pts = [UaEnv::Pluralization::sum_string(amount.to_i+1, 2, 'гривня', 'гривні', 'гривень')]
    else
      pts << UaEnv::Pluralization::sum_string(remainder.to_i, 2, 'копійка', 'копійки', 'копійок')
    end

  end
  pts.join(' ')
end

.sum_string(amount, gender, one_item = '', two_items = '', five_items = '') ⇒ Object

Виконує перетворення числа з цифрового виду в символьний

(amount, gender, one_item='', two_items='', five_items='')
  amount - числівник
  gender   = 1 - чоловічий, = 2 - жіночий, = 3 - середній
  one_item - називний відмінок однини (= 1)
  two_items - родовий відмінок однини (= 2-4)
  five_items - родовий відмінок множини ( = 5-10)


38
39
40
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
# File 'lib/pluralizer/pluralizer.rb', line 38

def self.sum_string(amount, gender, one_item='', two_items='', five_items='')
  into = ''
  tmp_val ||= 0

  return "нуль " + five_items if amount == 0

  tmp_val = amount

  # одиниці
  into, tmp_val = sum_string_fn(into, tmp_val, gender, one_item, two_items, five_items)

  return into if tmp_val == 0

  # тисячі
  into, tmp_val = sum_string_fn(into, tmp_val, 2, "тисяча", "тисячі", "тисяч") 

  return into if tmp_val == 0

  # мільйони
  into, tmp_val = sum_string_fn(into, tmp_val, 1, "мільйон", "мільйона", "мільйонів")

  return into if tmp_val == 0

  # мільярдів
  into, tmp_val = sum_string_fn(into, tmp_val, 1, "мільярд", "мільярда", "мільярдів")

  return into
end