Class: TwitterCldr::Tokenizers::NumberTokenizer

Inherits:
Base
  • Object
show all
Defined in:
lib/twitter_cldr/tokenizers/numbers/number_tokenizer.rb

Constant Summary collapse

ABBREVIATED_MIN_POWER =
3
ABBREVIATED_MAX_POWER =
14
VALID_TYPES =
[:decimal, :percent, :currency, :short_decimal, :long_decimal]
TOKEN_SPLITTER_REGEX =
/([^0*#,\.]*)([0#,\.]+)([^0*#,\.]*)$/
TOKEN_TYPE_REGEXES =
{
  :pattern => { :regex => /[0?#,\.]*/, :priority => 1 },
  :plaintext => { :regex => //, :priority => 2 }
}

Instance Attribute Summary

Attributes inherited from Base

#format, #locale, #paths, #placeholders, #resource, #token_splitter_regexes, #token_type_regexes, #type

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ NumberTokenizer

Returns a new instance of NumberTokenizer.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/twitter_cldr/tokenizers/numbers/number_tokenizer.rb', line 19

def initialize(options = {})
  super(options)

  @token_splitter_regexes = {
    :else => TOKEN_SPLITTER_REGEX
  }

  @token_type_regexes = {
    :else => TOKEN_TYPE_REGEXES
  }

  @base_path   = [:numbers, :formats]
  @symbol_path = [:numbers, :symbols]

  @paths = {
    :default       => [:decimal, :patterns],
    :decimal       => [:decimal, :patterns],
    :long_decimal  => [:decimal, :patterns, :long],
    :short_decimal => [:decimal, :patterns, :short],
    :currency      => [:currency, :patterns],
    :percent       => [:percent, :patterns]
  }
end

Instance Method Details

#symbolsObject



65
66
67
# File 'lib/twitter_cldr/tokenizers/numbers/number_tokenizer.rb', line 65

def symbols
  traverse(@symbol_path)
end

#tokens(options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/twitter_cldr/tokenizers/numbers/number_tokenizer.rb', line 43

def tokens(options = {})
  @type = options[:type] || :default
  @format = options[:format] || :default

  path = full_path
  positive, negative = traverse(path).to_s.split(/;/)
  sign = options[:sign] || :positive

  pattern = case sign
    when :negative
      if negative
        "#{symbols[:minus] || '-'}#{negative}"
      else
        "#{symbols[:minus] || '-'}#{positive}"
      end
    else
      positive
  end

  tokens_for_pattern(pattern, path, [sign])
end

#valid_typesObject



69
70
71
72
73
74
# File 'lib/twitter_cldr/tokenizers/numbers/number_tokenizer.rb', line 69

def valid_types
  VALID_TYPES.select do |type|
    result = traverse(@base_path + @paths[type])
    !!result ? result.size > 0 : result
  end
end