Class: TwitterCldr::Tokenizers::TimespanTokenizer

Inherits:
Base
  • Object
show all
Defined in:
lib/twitter_cldr/tokenizers/calendars/timespan_tokenizer.rb

Instance Attribute Summary

Attributes inherited from Base

#locale, #paths, #placeholders, #resource, #token_splitter_regex, #token_type_regexes, #type

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TimespanTokenizer

Returns a new instance of TimespanTokenizer.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/twitter_cldr/tokenizers/calendars/timespan_tokenizer.rb', line 9

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

  @type = options[:type] || :decimal

  @token_splitter_regex = /([^0*#,\.]*)([0#,\.]+)([^0*#,\.]*)$/ # creates spaces
  @token_type_regexes   = [
      { :type => :pattern, :regex => /[0?#,\.]*/ }, # splits token at right places
      { :type => :plaintext, :regex => // }
  ]
  @base_path = [:units]
  @paths = {
      :ago => {
          :default => :'hour-past',
          :second  => :'second-past',
          :minute  => :'minute-past',
          :hour    => :'hour-past',
          :day     => :'day-past',
          :week    => :'week-past',
          :month   => :'month-past',
          :year    => :'year-past'
      },
      :until => {
          :default => :'hour-future',
          :second  => :'second-future',
          :minute  => :'minute-future',
          :hour    => :'hour-future',
          :day     => :'day-future',
          :week    => :'week-future',
          :month   => :'month-future',
          :year    => :'year-future'
      }
  }
end

Instance Method Details

#token_exists(path) ⇒ Object



60
61
62
63
64
# File 'lib/twitter_cldr/tokenizers/calendars/timespan_tokenizer.rb', line 60

def token_exists(path)
  @@token_cache ||= {}
  cache_key = compute_cache_key(@locale, path.join('.'))
  true if @@token_cache.include?(cache_key) || traverse(path)
end

#tokens(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/twitter_cldr/tokenizers/calendars/timespan_tokenizer.rb', line 44

def tokens(options = {})
  path = full_path(options[:direction], options[:unit] || :default)
  pluralization = TwitterCldr::Formatters::Plurals::Rules.rule_for(options[:number], @locale)

  case pluralization # sometimes the plural rule will return ":one" when the resource only contains a path with "1"
    when :zero
      pluralization = 0 if token_exists(path + [0])
    when :one
      pluralization = 1 if token_exists(path + [1])
    when :two
      pluralization = 2 if token_exists(path + [2])
  end
  path += [pluralization]
  tokens_with_placeholders_for(path) if token_exists(path)
end