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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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', :group => "alt"),
    :fields   => fields,
    :formats => {
      :date     => formats('date'),
      :time     => formats('time'),
      :datetime => formats('dateTime')
    },
    :additional_formats => additional_formats
  )
end

Instance Method Details

#additional_formatsObject



127
128
129
130
131
132
133
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 127

def additional_formats
  select(calendar, "dateTimeFormats/availableFormats/dateFormatItem").inject({}) do |result, node|
    key = node.attribute('id').value
    result[key] = node.content
    result
  end
end

#calendarObject



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

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

#contexts(kind, options = {}) ⇒ Object



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

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

#default_format(type) ⇒ Object



135
136
137
138
139
140
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 135

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, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 44

def elements(node, kind, context, width, options = {})
  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

      if options[:group] && found_group = node.attribute(options[:group])
        result[found_group.value] ||= {}
        result[found_group.value][key] = node.content
      else
        result[key] = node.content
      end

      result
    end
  end
end

#erasObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 86

def eras
  if calendar
    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
  else
    {}
  end
end

#extract(path, lambdas) ⇒ Object



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

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

NOTE: As of CLDR 23, this data moved from inside each “calendar” tag to under its parent, the “dates” tag. That probably means this fields method should be moved up to the parent as well.



153
154
155
156
157
158
159
160
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 153

def fields
  select("dates/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



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 115

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



142
143
144
145
146
147
148
149
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 142

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



76
77
78
79
80
81
82
83
84
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 76

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, options = {}) ⇒ Object



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

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

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



66
67
68
69
70
71
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 66

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



73
74
# File 'lib/cldr/export/data/calendars/gregorian.rb', line 73

def xpath_width
end