Class: Sources::CLDR::SubdivisionUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/countries/sources/cldr/subdivision_updater.rb

Overview

Updates local subdivision files with data from the Unicode CLDR repository

Instance Method Summary collapse

Instance Method Details

#callObject



12
13
14
15
16
# File 'lib/countries/sources/cldr/subdivision_updater.rb', line 12

def call
  d = Dir['./tmp/cldr/trunk/common/subdivisions/*.xml']
  @loader = Sources::Local::CachedLoader.new(Sources::Local::Subdivision)
  d.each { |file_path| update_locale(file_path) }
end

#update_locale(file_path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/countries/sources/cldr/subdivision_updater.rb', line 18

def update_locale(file_path)
  language_data = Nokogiri::XML(File.read(file_path))
  language_code = File.basename(file_path, '.*')
  subdivisions = language_data.css('subdivision')
  return if subdivisions.empty?

  last_country_code_seen = nil

  subdivisions.each_with_index do |subdivision, index|
    subdivision = Sources::CLDR::Subdivision.new(language_code:, xml: subdivision)
    data = @loader.load(subdivision.country_code)
    data[subdivision.code] ||= {}
    data[subdivision.code] = data[subdivision.code].deep_merge(subdivision.to_h)
    if (last_country_code_seen && last_country_code_seen != subdivision.country_code) ||
       index == subdivisions.size - 1
      puts "Updated #{subdivision.country_code} with language_code #{language_code}"
      @loader.save(subdivision.country_code, data)
    end
    last_country_code_seen = subdivision.country_code
  end
end