Class: TwitterCldr::Shared::NumberingSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/shared/numbering_system.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, digits) ⇒ NumberingSystem

Returns a new instance of NumberingSystem.



59
60
61
62
# File 'lib/twitter_cldr/shared/numbering_system.rb', line 59

def initialize(name, digits)
  @name = name
  @digits = split_digits(digits)
end

Instance Attribute Details

#digitsObject (readonly)

Returns the value of attribute digits.



57
58
59
# File 'lib/twitter_cldr/shared/numbering_system.rb', line 57

def digits
  @digits
end

#nameObject (readonly)

Returns the value of attribute name.



57
58
59
# File 'lib/twitter_cldr/shared/numbering_system.rb', line 57

def name
  @name
end

Class Method Details

.for_locale(locale, format = :decimal) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/twitter_cldr/shared/numbering_system.rb', line 26

def for_locale(locale, format = :decimal)
  locale_cache[locale] ||= begin
    num_resource = TwitterCldr.get_locale_resource(locale, :numbers)

    system_name = TwitterCldr::Utils.traverse_hash(
      num_resource[locale], [:numbers, :formats, format, :number_system]
    )

    system_name ||= TwitterCldr::Utils.traverse_hash(
      num_resource[locale], [:numbers, :default_number_systems, :default]
    )

    for_name(system_name) if system_name
  end
end

.for_name(name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/twitter_cldr/shared/numbering_system.rb', line 14

def for_name(name)
  name_cache[name] ||= begin
    if system = resource[name.to_sym]
      if system[:type] != "numeric"
        raise UnsupportedNumberingSystemError.new("#{system[:type]} numbering systems not supported.")
      else
        new(system[:name], system[:digits])
      end
    end
  end
end

Instance Method Details

#transliterate(number) ⇒ Object



64
65
66
67
68
# File 'lib/twitter_cldr/shared/numbering_system.rb', line 64

def transliterate(number)
  number.to_s.gsub(/\d/) do |digit|
    digits[digit.to_i]
  end
end