Class: TwitterCldr::Localized::LocalizedTimespan

Inherits:
LocalizedObject show all
Defined in:
lib/twitter_cldr/localized/localized_timespan.rb

Constant Summary collapse

DEFAULT_TYPE =
:default
APPROXIMATE_MULTIPLIER =
0.75
TIME_IN_SECONDS =
{
  second: 1,
  minute: 60,
  hour:   3600,
  day:    86400,
  week:   604800,
  month:  2629743.83,
  year:   31556926
}

Instance Attribute Summary

Attributes inherited from LocalizedObject

#base_obj, #formatter, #locale

Instance Method Summary collapse

Methods inherited from LocalizedObject

localize

Constructor Details

#initialize(seconds, options = {}) ⇒ LocalizedTimespan

Returns a new instance of LocalizedTimespan.


23
24
25
# File 'lib/twitter_cldr/localized/localized_timespan.rb', line 23

def initialize(seconds, options = {})
  super(seconds, options[:locale] || TwitterCldr.locale, options)
end

Instance Method Details

#to_s(options = {}) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/twitter_cldr/localized/localized_timespan.rb', line 27

def to_s(options = {})
  unit = options[:unit] || calculate_unit(base_obj.abs, options)
  direction = options[:direction] || (base_obj < 0 ? :ago : :until)
  type = options[:type] || DEFAULT_TYPE
  number = calculate_time(base_obj, unit)

  data_reader = TwitterCldr::DataReaders::TimespanDataReader.new(locale, number, {
    unit: unit,
    direction: direction,
    type: type
  })

  tokens = data_reader.tokenizer.tokenize(data_reader.pattern)
  data_reader.formatter.format(tokens, number, options)
end