Class: TwitterCldr::Formatters::Rbnf::NormalRuleFormatter
- Inherits:
-
Object
- Object
- TwitterCldr::Formatters::Rbnf::NormalRuleFormatter
- Defined in:
- lib/twitter_cldr/formatters/numbers/rbnf/formatters.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#is_fractional ⇒ Object
readonly
Returns the value of attribute is_fractional.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
-
#omit ⇒ Object
readonly
Returns the value of attribute omit.
-
#rule_group ⇒ Object
readonly
Returns the value of attribute rule_group.
-
#rule_set ⇒ Object
readonly
Returns the value of attribute rule_set.
Instance Method Summary collapse
- #close_bracket(number, rule, token) ⇒ Object
-
#decimal(number, rule, token) ⇒ Object
if a decimal token occurs here, it’s actually plaintext.
- #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 (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 |
#locale ⇒ Object (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 |
#omit ⇒ Object (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_group ⇒ Object (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_set ⇒ Object (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 |