Class: TwitterCldr::Tokenizers::TimespanTokenizer

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

Constant Summary collapse

VALID_UNITS =
[:second, :minute, :hour, :day, :week, :month, :year]

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 = {}) ⇒ TimespanTokenizer

Returns a new instance of TimespanTokenizer.



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/twitter_cldr/tokenizers/calendars/timespan_tokenizer.rb', line 12

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

  @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'
    },
    :none => {
      :default => :second,
      :second  => :second,
      :minute  => :minute,
      :hour    => :hour,
      :day     => :day,
      :week    => :week,
      :month   => :month,
      :year    => :year
    }
  }
end

Instance Method Details

#all_types_for(unit, direction) ⇒ Object



83
84
85
# File 'lib/twitter_cldr/tokenizers/calendars/timespan_tokenizer.rb', line 83

def all_types_for(unit, direction)
  traverse(@base_path + [@paths[direction][unit]]).keys
end

#tokens(options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/twitter_cldr/tokenizers/calendars/timespan_tokenizer.rb', line 58

def tokens(options = {})
  # tokens_with_placeholders_for(full_path(:none, :second, :short, :two))
  path = full_path(options[:direction], options[:unit], options[:type])
  pluralization = options[:rule] || TwitterCldr::Formatters::Plurals::Rules.rule_for(options[:number], @locale)
  available = traverse(path)

  case pluralization # sometimes the plural rule will return ":one" when the resource only contains a path with "1"
    when :zero
      pluralization = 0 if available.include?(0)
    when :one
      pluralization = 1 if available.include?(1)
    when :two
      pluralization = 2 if available.include?(2)
  end

  if available.include?(pluralization)
    path << pluralization
  else
    return [] unless available.keys.first
    path << available.keys.first
  end

  tokens_with_placeholders_for(path)
end