Class: TwitterCldr::Formatters::Rbnf::RbnfFormatter

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

Constant Summary collapse

DEFAULT_SPELLOUT_OPTIONS =
{
  rule_group: "SpelloutRules",
  rule_set: "spellout-numbering"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale = TwitterCldr.locale) ⇒ RbnfFormatter

Returns a new instance of RbnfFormatter.



38
39
40
# File 'lib/twitter_cldr/formatters/numbers/rbnf.rb', line 38

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

Instance Attribute Details

#localeObject (readonly)

Returns the value of attribute locale.



36
37
38
# File 'lib/twitter_cldr/formatters/numbers/rbnf.rb', line 36

def locale
  @locale
end

Class Method Details

.supported_locale?(locale) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/twitter_cldr/formatters/numbers/rbnf.rb', line 32

def self.supported_locale?(locale)
  TwitterCldr.resource_exists?('locales', locale, 'rbnf')
end

Instance Method Details

#format(number, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/twitter_cldr/formatters/numbers/rbnf.rb', line 42

def format(number, options = {})
  rule_group_name, rule_set_name = *if options[:rule_group].nil? && options[:rule_set].nil?
    [DEFAULT_SPELLOUT_OPTIONS[:rule_group], DEFAULT_SPELLOUT_OPTIONS[:rule_set]]
  else
    [options[:rule_group], options[:rule_set]]
  end

  if rule_group = rule_group_by_name(rule_group_name)
    if rule_set = rule_group.rule_set_for(rule_set_name)
      if rule_set.public?
        RuleFormatter.format(number, rule_set, rule_group, locale)
      else
        raise PrivateRuleSetError.new(
          "#{rule_set_name} is a private rule set and cannot be used directly."
        )
      end
    end
  end
end

#group_namesObject



62
63
64
# File 'lib/twitter_cldr/formatters/numbers/rbnf.rb', line 62

def group_names
  @group_names ||= resource.map { |g| g[:type] }
end

#rule_set_names_for_group(group_name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/twitter_cldr/formatters/numbers/rbnf.rb', line 66

def rule_set_names_for_group(group_name)
  cache_key = TwitterCldr::Utils.compute_cache_key(locale, group_name)

  rule_set_name_cache[cache_key] ||= begin
    if rule_group = rule_group_by_name(group_name)
      rule_group.rule_sets.inject([]) do |ret, rule_set|
        ret << rule_set.name if rule_set.public?
        ret
      end
    end
  end

  rule_set_name_cache[cache_key] || []
end