class TwitterCldr::Formatters::Rbnf::Rule

Constants

IMPROPER_FRACTION
MASTER
NEGATIVE
PROPER_FRACTION
SUBSTITUTION_TYPES

Attributes

base_value[R]
locale[R]
radix[R]
rule_text[R]

Public Class Methods

new(base_value, rule_text, radix, locale) click to toggle source
# 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

Public Instance Methods

divisor() click to toggle source
# 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) click to toggle source
# File lib/twitter_cldr/formatters/numbers/rbnf/rule.rb, line 47
def even_multiple_of?(num)
  num % divisor == 0
end
improper_fraction?() click to toggle source
# File lib/twitter_cldr/formatters/numbers/rbnf/rule.rb, line 59
def improper_fraction?
  base_value == IMPROPER_FRACTION
end
master?() click to toggle source
# File lib/twitter_cldr/formatters/numbers/rbnf/rule.rb, line 55
def master?
  base_value == MASTER
end
negative?() click to toggle source
# File lib/twitter_cldr/formatters/numbers/rbnf/rule.rb, line 67
def negative?
  base_vaue == NEGATIVE
end
normal?() click to toggle source
# File lib/twitter_cldr/formatters/numbers/rbnf/rule.rb, line 51
def normal?
  !(master? || improper_fraction? || proper_fraction? || negative?)
end
proper_fraction?() click to toggle source
# File lib/twitter_cldr/formatters/numbers/rbnf/rule.rb, line 63
def proper_fraction?
  base_value == PROPER_FRACTION
end
substitution_count() click to toggle source
# 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() click to toggle source
# File lib/twitter_cldr/formatters/numbers/rbnf/rule.rb, line 71
def tokens
  @tokens ||= inline_substitutions(
    tokenizer.tokenize(rule_text)
  )
end

Private Instance Methods

inline_substitutions(tokens) click to toggle source
# File lib/twitter_cldr/formatters/numbers/rbnf/rule.rb, line 79
def inline_substitutions(tokens)
  parser.parse(tokens, locale: locale)
end
parser() click to toggle source
# File lib/twitter_cldr/formatters/numbers/rbnf/rule.rb, line 83
def parser
  @@parser ||= RuleParser.new
end
tokenizer() click to toggle source
# File lib/twitter_cldr/formatters/numbers/rbnf/rule.rb, line 87
def tokenizer
  @@tokenizer ||= TwitterCldr::Tokenizers::RbnfTokenizer.new
end