Class: TwitterCldr::Formatters::Rbnf::NormalRuleFormatter
- Inherits:
-
Object
- Object
- TwitterCldr::Formatters::Rbnf::NormalRuleFormatter
show all
- Defined in:
- lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#close_bracket(number, rule, token) ⇒ Object
-
#equals(number, rule, token) ⇒ Object
-
#format(number, rule) ⇒ Object
-
#generate_replacement(number, rule, token) ⇒ Object
-
#initialize(rule_set, rule_group, locale) ⇒ NormalRuleFormatter
constructor
A new instance of NormalRuleFormatter.
-
#left_arrow(number, rule, token) ⇒ Object
-
#open_bracket(number, rule, token) ⇒ Object
-
#plaintext(number, rule, token) ⇒ Object
-
#plural(number, rule, token) ⇒ Object
-
#right_arrow(number, rule, token) ⇒ Object
-
#semicolon(number, rule, token) ⇒ Object
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_fractional ⇒ Object
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
|
#locale ⇒ Object
Returns the value of attribute locale.
53
54
55
|
# File 'lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb', line 53
def locale
@locale
end
|
#omit ⇒ Object
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_group ⇒ Object
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_set ⇒ Object
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
|
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)
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
|