Class: Cldr::Export::Data::Calendars::Gregorian

Inherits:
Base
  • Object
show all
Defined in:
lib/cldr/export/data/calendars/gregorian.rb

Instance Attribute Summary

Attributes inherited from Base

#locale

Instance Method Summary collapse

Methods inherited from Base

#[]=, #update

Methods inherited from Hash

#deep_merge, #deep_stringify_keys, #symbolize_keys

Constructor Details

#initialize(locale) ⇒ Gregorian

Returns a new instance of Gregorian.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 6

def initialize(locale)
  super
  update(
    :months   => contexts('month'),
    :days     => contexts('day'),
    :eras     => eras,
    :quarters => contexts('quarter'),
    :periods  => contexts('dayPeriod'),
    :fields   => fields,
    :formats => {
      :date     => formats('date'),
      :time     => formats('time'),
      :datetime => formats('dateTime')
    }
  )
end

Instance Method Details

#calendarObject



23
24
25
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 23

def calendar
  @calendar ||= select('dates/calendars/calendar[@type="gregorian"]').first
end

#contexts(kind) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 27

def contexts(kind)
  select(calendar, "#{kind}s/#{kind}Context").inject({}) do |result, node|
    context = node.attribute('type').value.to_sym
    result[context] = widths(node, kind, context)
    result
  end
end

#default_format(type) ⇒ Object



114
115
116
117
118
119
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 114

def default_format(type)
  if node = select(calendar, "#{type}Formats/default").first
    key = node.attribute('choice').value.to_sym
    { :default => :"calendars.gregorian.formats.#{type.downcase}.#{key}" }
  end
end

#elements(node, kind, context, width) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 43

def elements(node, kind, context, width)
  aliased = select(node, 'alias').first
  if aliased
    xpath_to_key(aliased.attribute('path').value, kind, context, width)
  else
    select(node, kind).inject({}) do |result, node|
      key = node.attribute('type').value
      key = key =~ /^\d*$/ ? key.to_i : key.to_sym
      result[key] = node.content
      result
    end
  end
end

#erasObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 77

def eras
  base_path = calendar.path.gsub('/ldml/', '') + '/eras'
  keys = select("#{base_path}/*").map { |node| node.name }

  keys.inject({}) do |result, name|
    path = "#{base_path}/#{name}/*"
    key  = name.gsub('era', '').gsub(/s$/, '').downcase.to_sym
    result[key] = select(path).inject({}) do |ret, node|
      type = node.attribute('type').value.to_i rescue 0
      ret[type] = node.content
      ret
    end
    result
  end
end

#extract(path, lambdas) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 93

def extract(path, lambdas)
  nodes = select(path)
  nodes.inject({}) do |ret, node|
    key = lambdas[:key].call(node)
    ret[key] = lambdas[:value].call(node)
    ret
  end
end

#fieldsObject



130
131
132
133
134
135
136
137
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 130

def fields
  select(calendar, "fields/field").inject({}) do |result, node|
    key  = node.attribute('type').value.to_sym
    name = node.xpath('displayName').first
    result[key] = name.content if name
    result
  end
end

#formats(type) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 102

def formats(type)
  formats = select(calendar, "#{type}Formats/#{type}FormatLength").inject({}) do |result, node|
    key = node.attribute('type').value.to_sym rescue :format
    result[key] = pattern(node, type)
    result
  end
  if default = default_format(type)
    formats = default.merge(formats)
  end
  formats
end

#pattern(node, type) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 121

def pattern(node, type)
  select(node, "#{type}Format/pattern").inject({}) do |result, node|
    pattern = node.content
    pattern = pattern.gsub('{0}', '{{time}}').gsub('{1}', '{{date}}') if type == 'dateTime'
    result[:pattern] = pattern
    result
  end
end

#periodsObject



67
68
69
70
71
72
73
74
75
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 67

def periods
  am = select(calendar, "am").first
  pm = select(calendar, "pm").first

  result = {}
  result[:am] = am.content if am
  result[:pm] = pm.content if pm
  result
end

#widths(node, kind, context) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 35

def widths(node, kind, context)
  select(node, "#{kind}Width").inject({}) do |result, node|
    width  = node.attribute('type').value.to_sym
    result[width] = elements(node, kind, context, width)
    result
  end
end

#xpath_to_key(xpath, kind, context, width) ⇒ Object



57
58
59
60
61
62
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 57

def xpath_to_key(xpath, kind, context, width)
  kind    = (xpath =~ %r(/([^\/]*)Width) && $1) || kind
  context = (xpath =~ %r(Context\[@type='([^\/]*)'\]) && $1) || context
  width   = (xpath =~ %r(Width\[@type='([^\/]*)'\]) && $1) || width
  :"calendars.gregorian.#{kind}s.#{context}.#{width}"
end

#xpath_widthObject



64
65
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 64

def xpath_width
end