Class: TwitterCldr::Formatters::Rbnf::Rule

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_valueObject (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

#localeObject (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

#radixObject (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_textObject (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

#divisorObject



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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


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

def master?
  base_value == MASTER
end

#negative?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/twitter_cldr/formatters/numbers/rbnf/rule.rb', line 67

def negative?
  base_vaue == NEGATIVE
end

#normal?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


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

def proper_fraction?
  base_value == PROPER_FRACTION
end

#substitution_countObject



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

#tokensObject



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