Class: TwitterCldr::Formatters::Rbnf::Rule
- Inherits:
-
Object
- Object
- TwitterCldr::Formatters::Rbnf::Rule
- Defined in:
- lib/twitter_cldr/formatters/numbers/rbnf/rule.rb
Constant Summary collapse
- SUBSTITUTION_TYPES =
[:equals, :left_arrow, :right_arrow]
- MASTER =
"x.0"
- IMPROPER_FRACTION =
"x.x"
- PROPER_FRACTION =
"0.x"
- NEGATIVE =
"-x"
Instance Attribute Summary collapse
-
#base_value ⇒ Object
readonly
Returns the value of attribute base_value.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
-
#radix ⇒ Object
readonly
Returns the value of attribute radix.
-
#rule_text ⇒ Object
readonly
Returns the value of attribute rule_text.
Instance Method Summary collapse
- #divisor ⇒ Object
- #even_multiple_of?(num) ⇒ Boolean
- #improper_fraction? ⇒ Boolean
-
#initialize(base_value, rule_text, radix, locale) ⇒ Rule
constructor
A new instance of Rule.
- #master? ⇒ Boolean
- #negative? ⇒ Boolean
- #normal? ⇒ Boolean
- #proper_fraction? ⇒ Boolean
- #substitution_count ⇒ Object
- #tokens ⇒ Object
Constructor Details
#initialize(base_value, rule_text, radix, locale) ⇒ Rule
Returns a new instance of Rule.
19 20 21 22 23 24 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 19 def initialize(base_value, rule_text, radix, locale) @base_value = base_value @rule_text = rule_text @radix = radix @locale = locale end |
Instance Attribute Details
#base_value ⇒ Object (readonly)
Returns the value of attribute base_value.
17 18 19 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 17 def base_value @base_value end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
17 18 19 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 17 def locale @locale end |
#radix ⇒ Object (readonly)
Returns the value of attribute radix.
17 18 19 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 17 def radix @radix end |
#rule_text ⇒ Object (readonly)
Returns the value of attribute rule_text.
17 18 19 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 17 def rule_text @rule_text end |
Instance Method Details
#divisor ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 26 def divisor @divisor ||= begin val = base_value.to_i exp = val > 0 ? (Math.log(val) / Math.log(radix || 10)).ceil : 1 div = exp >= 0 ? (radix || 10) ** exp : 1 # if result is too big, subtract one from exponent and try again if div > val (radix || 10) ** (exp - 1) else div end end end |
#even_multiple_of?(num) ⇒ Boolean
47 48 49 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 47 def even_multiple_of?(num) num % divisor == 0 end |
#improper_fraction? ⇒ Boolean
59 60 61 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 59 def improper_fraction? base_value == IMPROPER_FRACTION end |
#master? ⇒ Boolean
55 56 57 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 55 def master? base_value == MASTER end |
#negative? ⇒ Boolean
67 68 69 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 67 def negative? base_vaue == NEGATIVE end |
#normal? ⇒ Boolean
51 52 53 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 51 def normal? !(master? || improper_fraction? || proper_fraction? || negative?) end |
#proper_fraction? ⇒ Boolean
63 64 65 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 63 def proper_fraction? base_value == PROPER_FRACTION end |
#substitution_count ⇒ Object
41 42 43 44 45 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 41 def substitution_count @substitution_count ||= tokens.inject(0) do |ret, token| token.is_a?(Substitution) ? ret + 1 : ret end end |
#tokens ⇒ Object
71 72 73 74 75 |
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 71 def tokens @tokens ||= inline_substitutions( tokenizer.tokenize(rule_text) ) end |