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



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

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

#decimal(number, rule, token) ⇒ Object

if a decimal token occurs here, it’s actually plaintext



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

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

#equals(number, rule, token) ⇒ Object



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

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



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

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
    sign = number < 0 ? :negative : :positive
    @data_reader ||= TwitterCldr::DataReaders::NumberDataReader.new(locale)
    decimal_format = @data_reader.pattern_for_sign(decimal_format, sign)
    @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



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

def left_arrow(number, rule, token)
  quotient = (number.abs / rule.divisor) * (number < 0 ? -1 : 1)
  generate_replacement(quotient, rule, token)
end

#open_bracket(number, rule, token) ⇒ Object



107
108
109
110
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 107

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

#plaintext(number, rule, token) ⇒ Object



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

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

#plural(number, rule, token) ⇒ Object



130
131
132
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 130

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

#right_arrow(number, rule, token) ⇒ Object



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

def right_arrow(number, rule, token)
  prev_rule = rule_set.previous_rule_for(rule) if token.length == 3
  remainder = (number.abs % rule.divisor) * (number < 0 ? -1 : 1)
  generate_replacement(remainder, prev_rule, token)
end

#semicolon(number, rule, token) ⇒ Object



126
127
128
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 126

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