Class: TwitterCldr::DataReaders::TimespanDataReader

Inherits:
DataReader
  • Object
show all
Defined in:
lib/twitter_cldr/data_readers/timespan_data_reader.rb

Constant Summary collapse

DEFAULT_DIRECTION =
:none
DEFAULT_TYPE =
:default
VALID_UNITS =
[:second, :minute, :hour, :day, :week, :month, :year]
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
  }
}

Instance Attribute Summary collapse

Attributes inherited from DataReader

#locale

Instance Method Summary collapse

Methods inherited from DataReader

#pattern_at_path

Constructor Details

#initialize(locale, seconds, options = {}) ⇒ TimespanDataReader

Returns a new instance of TimespanDataReader.



50
51
52
53
54
55
56
57
58
59
# File 'lib/twitter_cldr/data_readers/timespan_data_reader.rb', line 50

def initialize(locale, seconds, options = {})
  super(locale)

  @type = options[:type] || DEFAULT_TYPE
  @direction = options[:direction] || DEFAULT_DIRECTION
  @unit = options[:unit]

  @rule = options[:rule] ||
    TwitterCldr::Formatters::Plurals::Rules.rule_for(seconds, locale)
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



48
49
50
# File 'lib/twitter_cldr/data_readers/timespan_data_reader.rb', line 48

def direction
  @direction
end

#ruleObject (readonly)

Returns the value of attribute rule.



48
49
50
# File 'lib/twitter_cldr/data_readers/timespan_data_reader.rb', line 48

def rule
  @rule
end

#typeObject (readonly)

Returns the value of attribute type.



48
49
50
# File 'lib/twitter_cldr/data_readers/timespan_data_reader.rb', line 48

def type
  @type
end

#unitObject (readonly)

Returns the value of attribute unit.



48
49
50
# File 'lib/twitter_cldr/data_readers/timespan_data_reader.rb', line 48

def unit
  @unit
end

Instance Method Details

#all_typesObject



85
86
87
# File 'lib/twitter_cldr/data_readers/timespan_data_reader.rb', line 85

def all_types
  traverse(BASE_PATH + [PATHS[direction][unit]]).keys
end

#formatterObject



81
82
83
# File 'lib/twitter_cldr/data_readers/timespan_data_reader.rb', line 81

def formatter
  TwitterCldr::Formatters::TimespanFormatter.new(self)
end

#patternObject

type is stuff like :abbreviated, etc



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/twitter_cldr/data_readers/timespan_data_reader.rb', line 62

def pattern
  path = full_path_for(direction, unit, type)
  available = traverse(path)
  pluralization = pluralization_for(rule, available)

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

  traverse(path)
end

#tokenizerObject



77
78
79
# File 'lib/twitter_cldr/data_readers/timespan_data_reader.rb', line 77

def tokenizer
  TwitterCldr::Tokenizers::TimespanTokenizer.new(self)
end