Class: SgtnClient::Formatters::PluralFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/sgtn-client/formatters/plurals/plural_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale = TwitterCldr.locale) ⇒ PluralFormatter

Returns a new instance of PluralFormatter.



12
13
14
# File 'lib/sgtn-client/formatters/plurals/plural_formatter.rb', line 12

def initialize(locale = TwitterCldr.locale)
  @locale = TwitterCldr.convert_locale(locale)
end

Instance Attribute Details

#localeObject (readonly)

Returns the value of attribute locale.



10
11
12
# File 'lib/sgtn-client/formatters/plurals/plural_formatter.rb', line 10

def locale
  @locale
end

Instance Method Details

#num_s(string, replacements) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sgtn-client/formatters/plurals/plural_formatter.rb', line 16

def num_s(string, replacements)
  reg = Regexp.union(
    /%<(\{.*?\})>/
  )
  string.gsub(reg) do
    count_placeholder, patterns = if $1
      pluralization_hash = JSON.parse($1)
      if pluralization_hash.is_a?(Hash) && pluralization_hash.size == 1
        pluralization_hash.first
      else
        raise ArgumentError.new('expected a Hash with a single key')
      end
    else
      raise ArgumentError.new('invalide format')
    end
    count  = replacements[count_placeholder.to_sym].to_s
    if patterns.is_a?(Hash)
      return TwitterCldr::Utils.deep_symbolize_keys(patterns)[count.to_sym]
    else
      raise ArgumentError.new('expected patterns to be a Hash')
    end
  end
end