Class: TwitterCldr::Formatters::Rbnf::Plural

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/formatters/numbers/rbnf/plural.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale, plural_type, cases) ⇒ Plural

plural_type = cardinal, etc cases = hash of form one: “foo”, two: “bar”



28
29
30
31
32
# File 'lib/twitter_cldr/formatters/numbers/rbnf/plural.rb', line 28

def initialize(locale, plural_type, cases)
  @locale = locale
  @plural_type = plural_type
  @cases = cases
end

Instance Attribute Details

#casesObject (readonly)

Returns the value of attribute cases.



24
25
26
# File 'lib/twitter_cldr/formatters/numbers/rbnf/plural.rb', line 24

def cases
  @cases
end

#localeObject (readonly)

Returns the value of attribute locale.



24
25
26
# File 'lib/twitter_cldr/formatters/numbers/rbnf/plural.rb', line 24

def locale
  @locale
end

#plural_typeObject (readonly)

Returns the value of attribute plural_type.



24
25
26
# File 'lib/twitter_cldr/formatters/numbers/rbnf/plural.rb', line 24

def plural_type
  @plural_type
end

Class Method Details

.from_string(locale, str) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/twitter_cldr/formatters/numbers/rbnf/plural.rb', line 11

def self.from_string(locale, str)
  # $(cardinal,one{ tysiąc}few{ tysiące}other{ tysięcy})$
  plural_type, cases = str.gsub(/\$\((.*)\)\$/, '\1').split(',')

  # one{ tysiąc}few{ tysiące}other{ tysięcy}
  cases = cases.scan(/([\w]+)\{([^}]+)\}/).inject({}) do |ret, case_arr|
    ret[case_arr.first.to_sym] = case_arr.last
    ret
  end

  new(locale, plural_type.to_sym, cases)
end

Instance Method Details

#render(number) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/twitter_cldr/formatters/numbers/rbnf/plural.rb', line 34

def render(number)
  rule_name = TwitterCldr::Formatters::Plurals::Rules.rule_for(
    number, locale, plural_type
  )

  cases[rule_name] || cases[:other]
end

#typeObject



42
43
44
# File 'lib/twitter_cldr/formatters/numbers/rbnf/plural.rb', line 42

def type
  :plural
end