Class: AmountInflector

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

Constant Summary collapse

CONFIG =
{
   :godina => { one:"godina", few:"godine",  many:"godina" },
   :mjesec => { one:"mjesec", few:"mjeseca", many:"mjeseci" },
   :tjedan => { one:"tjedan", few:"tjedna",  many:"tjedana" },
   :dan =>    { one:"dan",    few:"dana",    many:"dana" },
   :kuna =>   { one:"kuna",   few:"kune",    many:"kuna" },
   :lipa =>   { one:"lipa",   few:"lipe",    many:"lipa" },
   :tisuca => { one:"tisuću", few:"tisuće",  many:"tisuća" },
   :milijun => { one:"milijun", few:"milijuna",  many:"milijuna" },
   :milijarda => { one:"milijarda", few:"milijarde",  many:"milijardi" },
   :promet => { one:"promet", few:"prometa",  many:"prometa" }
}

Class Method Summary collapse

Class Method Details

.inflect(amount, unit) ⇒ Object



17
18
19
20
21
# File 'lib/amount_inflector/amount_inflector.rb', line 17

def self.inflect(amount, unit)
  unit = unit.to_s.to_sym
  raise "Inflection :#{unit} is unsupported" if CONFIG[unit].nil?
  "#{amount} #{CONFIG[unit][pluralize_form(amount)]}"
end

.inflect_unit(amount, unit) ⇒ Object



23
24
25
26
27
# File 'lib/amount_inflector/amount_inflector.rb', line 23

def self.inflect_unit(amount, unit)
  unit = unit.to_s.to_sym
  raise "Inflection :#{unit} is unsupported" if CONFIG[unit].nil?
  "#{CONFIG[unit][pluralize_form(amount)]}"
end

.pluralize_form(n) ⇒ Object



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

def self.pluralize_form(n)
  return :many if (11..14).include?(n % 100)
  return :few if (2..4).include?(n % 10)
  (n % 10 == 1) ? :one : :many
end