Class: TwitterCldr::Resources::ListFormats

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/resources/list_formats_importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale, cldr_req) ⇒ ListFormats

Returns a new instance of ListFormats.



79
80
81
82
# File 'lib/twitter_cldr/resources/list_formats_importer.rb', line 79

def initialize(locale, cldr_req)
  @locale = locale
  @cldr_req = cldr_req
end

Instance Attribute Details

#cldr_reqObject (readonly)

Returns the value of attribute cldr_req.



77
78
79
# File 'lib/twitter_cldr/resources/list_formats_importer.rb', line 77

def cldr_req
  @cldr_req
end

#localeObject (readonly)

Returns the value of attribute locale.



77
78
79
# File 'lib/twitter_cldr/resources/list_formats_importer.rb', line 77

def locale
  @locale
end

Instance Method Details

#cldr_main_pathObject



133
134
135
# File 'lib/twitter_cldr/resources/list_formats_importer.rb', line 133

def cldr_main_path
  @cldr_main_path ||= File.join(cldr_req.common_path, 'main')
end

#docObject



126
127
128
129
130
131
# File 'lib/twitter_cldr/resources/list_formats_importer.rb', line 126

def doc
  @doc ||= begin
    locale_fs = locale.to_s.gsub('-', '_')
    Nokogiri.XML(File.read(File.join(cldr_main_path, "#{locale_fs}.xml")))
  end
end

#listsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/twitter_cldr/resources/list_formats_importer.rb', line 88

def lists
  doc.xpath('//ldml/listPatterns/listPattern').each_with_object({}) do |pattern_node, pattern_result|
    pattern_type = if attribute = pattern_node.attribute('type')
      attribute.value.to_sym
    else
      :default
    end

    pattern_node = pattern_for(pattern_type)

    pattern_result[pattern_type] = pattern_node.xpath('listPatternPart').each_with_object({}) do |type_node, type_result|
      type_result[type_node.attribute('type').value.to_sym] = type_node.content
    end
  end
end

#pattern_for(type) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/twitter_cldr/resources/list_formats_importer.rb', line 104

def pattern_for(type)
  xpath = xpath_for(type)
  pattern_node = doc.xpath(xpath)[0]
  alias_node = pattern_node.xpath('alias')[0]

  if alias_node
    alias_type = alias_node.attribute('path').value[/@type='([\w-]+)'/, 1] || :default
    # follow aliases so we can fully expand them
    pattern_node = pattern_for(alias_type)
  end

  pattern_node
end

#to_hObject



84
85
86
# File 'lib/twitter_cldr/resources/list_formats_importer.rb', line 84

def to_h
  { lists: lists }
end

#xpath_for(type) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/twitter_cldr/resources/list_formats_importer.rb', line 118

def xpath_for(type)
  if type == :default
    '//ldml/listPatterns/listPattern[not(@type)]'
  else
    "//ldml/listPatterns/listPattern[@type='#{type}']"
  end
end