Class: TwitterCldr::Formatters::Rbnf::NormalRuleFormatter

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

Direct Known Subclasses

MasterRuleFormatter, NegativeRuleFormatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rule_set, rule_group, locale) ⇒ NormalRuleFormatter

Returns a new instance of NormalRuleFormatter.



55
56
57
58
59
60
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 55

def initialize(rule_set, rule_group, locale)
  @rule_set = rule_set
  @rule_group = rule_group
  @is_fractional = false
  @locale = locale
end

Instance Attribute Details

#is_fractionalObject (readonly)

Returns the value of attribute is_fractional.



53
54
55
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 53

def is_fractional
  @is_fractional
end

#localeObject (readonly)

Returns the value of attribute locale.



53
54
55
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 53

def locale
  @locale
end

#omitObject (readonly)

Returns the value of attribute omit.



53
54
55
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 53

def omit
  @omit
end

#rule_groupObject (readonly)

Returns the value of attribute rule_group.



53
54
55
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 53

def rule_group
  @rule_group
end

#rule_setObject (readonly)

Returns the value of attribute rule_set.



53
54
55
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 53

def rule_set
  @rule_set
end

Instance Method Details

#close_bracket(number, rule, token) ⇒ Object



109
110
111
112
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 109

def close_bracket(number, rule, token)
  @omit = false
  ""
end

#equals(number, rule, token) ⇒ Object



81
82
83
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 81

def equals(number, rule, token)
  generate_replacement(number, rule, token)
end

#format(number, rule) ⇒ Object



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

def format(number, rule)
  rule.tokens.map do |token|
    result = send(token.type, number, rule, token)
    @omit ? "" : result
  end.join
end

#generate_replacement(number, rule, token) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 85

def generate_replacement(number, rule, token)
  if rule_set_name = token.rule_set_reference
    RuleFormatter.format(
      number,
      rule_group.rule_set_for(rule_set_name),
      rule_group,
      locale
    )
  elsif decimal_format = token.decimal_format
    @data_reader ||= TwitterCldr::DataReaders::NumberDataReader.new(locale)
    @decimal_tokenizer ||= TwitterCldr::Tokenizers::NumberTokenizer.new(@data_reader)
    decimal_tokens = @decimal_tokenizer.tokenize(decimal_format)
    @decimal_formatter ||= TwitterCldr::Formatters::NumberFormatter.new(@data_reader)
    @decimal_formatter.format(decimal_tokens, number, type: :decimal)
  else
    RuleFormatter.format(number, rule_set, rule_group, locale)
  end
end

#left_arrow(number, rule, token) ⇒ Object



76
77
78
79
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 76

def left_arrow(number, rule, token)
  quotient = number.abs / rule.divisor
  generate_replacement(quotient, rule, token)
end

#open_bracket(number, rule, token) ⇒ Object



104
105
106
107
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 104

def open_bracket(number, rule, token)
  @omit = rule.even_multiple_of?(number)
  ""
end

#plaintext(number, rule, token) ⇒ Object



114
115
116
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 114

def plaintext(number, rule, token)
  token.value
end

#plural(number, rule, token) ⇒ Object



122
123
124
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 122

def plural(number, rule, token)
  token.render(number / rule.divisor)
end

#right_arrow(number, rule, token) ⇒ Object



69
70
71
72
73
74
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 69

def right_arrow(number, rule, token)
  # this seems to break things even though the docs require it:
  # rule = rule_set.previous_rule_for(rule) if token.length == 3
  remainder = number.abs % rule.divisor
  generate_replacement(remainder, rule, token)
end

#semicolon(number, rule, token) ⇒ Object



118
119
120
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 118

def semicolon(number, rule, token)
  ""
end