class TwitterCldr::Shared::Calendar

Constants

DATETIME_METHOD_MAP
DEFAULT_FORMAT
ERAS_NAMES_FORMS
NAMES_FORMS
REDIRECT_CONVERSIONS

Attributes

calendar_type[R]
locale[R]

Public Class Methods

new(locale = TwitterCldr.locale, calendar_type = TwitterCldr::DEFAULT_CALENDAR_TYPE) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 34
def initialize(locale = TwitterCldr.locale, calendar_type = TwitterCldr::DEFAULT_CALENDAR_TYPE)
  @locale = TwitterCldr.convert_locale(locale)
  @calendar_type = calendar_type
end

Public Instance Methods

calendar_data() click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 88
def calendar_data
  @calendar_data ||= TwitterCldr::Utils.traverse_hash(resource, [locale, :calendars, calendar_type])
end
date_order(options = {}) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 76
def date_order(options = {})
  get_order_for(TwitterCldr::DataReaders::DateDataReader, options)
end
datetime_order(options = {}) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 84
def datetime_order(options = {})
  get_order_for(TwitterCldr::DataReaders::DateTimeDataReader, options)
end
eras(names_form = :name) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 70
def eras(names_form = :name)
  cache_field_data(:eras, names_form) do
    get_data(:eras)[names_form]
  end
end
fields() click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 52
def fields
  cache_field_data(:fields) do
    get_data(:fields)
  end
end
months(names_form = :wide, format = DEFAULT_FORMAT) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 39
def months(names_form = :wide, format = DEFAULT_FORMAT)
  cache_field_data(:months, names_form, format) do
    data = get_with_names_form(:months, names_form, format)
    data && data.sort_by { |m| m.first }.map { |m| m.last }
  end
end
periods(names_form = :wide, format = DEFAULT_FORMAT) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 64
def periods(names_form = :wide, format = DEFAULT_FORMAT)
  cache_field_data(:periods, names_form, format) do
    get_with_names_form(:periods, names_form, format)
  end
end
quarters(names_form = :wide, format = DEFAULT_FORMAT) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 58
def quarters(names_form = :wide, format = DEFAULT_FORMAT)
  cache_field_data(:quarters, names_form, format) do
    get_with_names_form(:quarters, names_form, format)
  end
end
time_order(options = {}) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 80
def time_order(options = {})
  get_order_for(TwitterCldr::DataReaders::TimeDataReader, options)
end
weekdays(names_form = :wide, format = DEFAULT_FORMAT) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 46
def weekdays(names_form = :wide, format = DEFAULT_FORMAT)
  cache_field_data(:weekdays, names_form, format) do
    get_with_names_form(:days, names_form, format)
  end
end

Private Instance Methods

cache_field_data(field, names_form = nil, format = nil) { || ... } click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 94
def cache_field_data(field, names_form = nil, format = nil)
  cache_key = TwitterCldr::Utils.compute_cache_key(locale, field, names_form, format)
  field_cache[cache_key] ||= begin
    yield
  end
end
calendar_cache() click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 105
def calendar_cache
  @@calendar_cache ||= {}
end
day_periods_cache() click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 109
def day_periods_cache
  @@day_periods_cache ||= {}
end
field_cache() click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 101
def field_cache
  @@field_cache ||= {}
end
get_data(*path) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 140
def get_data(*path)
  cache_key = TwitterCldr::Utils.compute_cache_key([@locale] + path)
  calendar_cache.fetch(cache_key) do |key|
    data = TwitterCldr::Utils.traverse_hash(calendar_data, path)
    redirect = parse_redirect(data)
    calendar_cache[key] = if redirect
      get_data(*redirect)
    else
      data
    end
  end
end
get_order_for(data_reader_const, options) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 113
def get_order_for(data_reader_const, options)
  key_array = [data_reader_const.to_s, @locale] + options.keys.sort + options.values.sort
  cache_key = TwitterCldr::Utils.compute_cache_key(key_array)
  calendar_cache.fetch(cache_key) do |key|
    data_reader = data_reader_const.new(@locale, options)
    tokens = data_reader.tokenizer.tokenize(data_reader.pattern)
    calendar_cache[cache_key] = resolve_methods(methods_for_tokens(tokens))
  end
end
get_with_names_form(data_type, names_form, format) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 136
def get_with_names_form(data_type, names_form, format)
  get_data(data_type, format, names_form) if NAMES_FORMS.include?(names_form.to_sym)
end
methods_for_tokens(tokens) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 127
def methods_for_tokens(tokens)
  tokens.inject([]) do |ret, token|
    if token.type == :pattern
      ret << TwitterCldr::Formatters::DateTimeFormatter::METHODS[token.value[0].chr]
    end
    ret
  end
end
parse_redirect(data) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 153
def parse_redirect(data)
  if data.is_a?(Symbol) && data.to_s =~ redirect_regexp
    result = $1.split('.').map(&:to_sym)
    result.map { |leg| REDIRECT_CONVERSIONS.fetch(leg, leg) }
  end
end
redirect_regexp() click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 160
def redirect_regexp
  Regexp.new("^calendars\.#{calendar_type}\.(.*)$")
end
resolve_methods(methods) click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 123
def resolve_methods(methods)
  methods.map { |method| DATETIME_METHOD_MAP.fetch(method, method) }
end
resource() click to toggle source
# File lib/twitter_cldr/shared/calendar.rb, line 164
def resource
  TwitterCldr.get_locale_resource(@locale, :calendars)
end