class TwitterCldr::Resources::Units

Attributes

cldr_req[R]
locale[R]

Public Class Methods

new(locale, cldr_req) click to toggle source
# File lib/twitter_cldr/resources/units_importer.rb, line 65
def initialize(locale, cldr_req)
  @locale = locale
  @cldr_req = cldr_req
end

Public Instance Methods

to_h() click to toggle source
# File lib/twitter_cldr/resources/units_importer.rb, line 70
def to_h
  {
    units: {
      unitLength: unit_length,
      durationUnit: duration_unit
    }
  }
end

Private Instance Methods

cldr_main_path() click to toggle source
# File lib/twitter_cldr/resources/units_importer.rb, line 126
def cldr_main_path
  @cldr_main_path ||= File.join(cldr_req.common_path, 'main')
end
doc() click to toggle source
# File lib/twitter_cldr/resources/units_importer.rb, line 119
def doc
  @doc ||= begin
    locale_fs = locale.to_s.gsub('-', '_')
    Nokogiri.XML(File.read(File.join(cldr_main_path, "#{locale_fs}.xml")))
  end
end
duration_unit() click to toggle source
# File lib/twitter_cldr/resources/units_importer.rb, line 113
def duration_unit
  doc.xpath('//ldml/units/durationUnit').each_with_object({}) do |node, result|
    result[node.attribute('type').value.to_sym] = node.xpath('durationUnitPattern').first.content
  end
end
resolve_unit_node(root, unit_node) click to toggle source
# File lib/twitter_cldr/resources/units_importer.rb, line 88
def resolve_unit_node(root, unit_node)
  alias_node = unit_node.xpath('alias')[0]
  return unit_node unless alias_node

  # follow aliases so we can fully expand them
  alias_type = alias_node.attribute('path').value[/@type='([\w-]+)'/, 1]
  found_node = root.xpath("unit[@type='#{alias_type}']")
  resolve_unit_node(root, found_node)
end
unit(node) click to toggle source
# File lib/twitter_cldr/resources/units_importer.rb, line 106
def unit(node)
  node.xpath('unitPattern').each_with_object({}) do |node, result|
    count = node.attribute('count') ? node.attribute('count').value.to_sym : :one
    result[count] = node.content
  end
end
unit_length() click to toggle source
# File lib/twitter_cldr/resources/units_importer.rb, line 81
def unit_length
  doc.xpath('//ldml/units/unitLength').each_with_object({}) do |node, result|
    type = node.attribute('type').value.to_sym
    result[type] = units(node)
  end
end
units(node) click to toggle source
# File lib/twitter_cldr/resources/units_importer.rb, line 98
def units(node)
  node.xpath('unit').each_with_object({}) do |unit_node, result|
    unit_node = resolve_unit_node(node, unit_node)
    type = unit_node.attribute('type').value.to_sym
    result[type] = unit(unit_node)
  end
end