Class: A0::TZMigration::DataGenerator
- Inherits:
-
Object
- Object
- A0::TZMigration::DataGenerator
- Defined in:
- lib/a0/tzmigration/data_generator.rb,
lib/a0/tzmigration/data_generator/git.rb,
lib/a0/tzmigration/data_generator/save.rb,
lib/a0/tzmigration/data_generator/process.rb
Instance Attribute Summary collapse
-
#out ⇒ Object
readonly
Returns the value of attribute out.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#timezones ⇒ Object
readonly
Returns the value of attribute timezones.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#versions ⇒ Object
readonly
Returns the value of attribute versions.
Instance Method Summary collapse
- #data_from_transition(transition) ⇒ Object
- #generate ⇒ Object
- #generate_timezones ⇒ Object
- #generate_versions ⇒ Object
-
#initialize(out, url: 'https://github.com/tzinfo/tzinfo-data.git') ⇒ DataGenerator
constructor
A new instance of DataGenerator.
- #load_release_dates ⇒ Object
- #process(tag) ⇒ Object
- #process_tag(tag) ⇒ Object
- #process_timezone(timezones, real_timezone) ⇒ Object
- #repo ⇒ Object
- #repo_clone ⇒ Object
- #repo_update ⇒ Object
- #repo_use(tag) ⇒ Object
- #save_index ⇒ Object
- #save_timezones ⇒ Object
- #save_timezones_index ⇒ Object
- #save_versions ⇒ Object
- #save_versions_index ⇒ Object
- #timezones_row(timezone_name) ⇒ Object
- #tzinfo_load ⇒ Object
- #tzinfo_unload ⇒ Object
- #version_head ⇒ Object
Constructor Details
#initialize(out, url: 'https://github.com/tzinfo/tzinfo-data.git') ⇒ DataGenerator
Returns a new instance of DataGenerator.
12 13 14 15 16 17 18 19 20 |
# File 'lib/a0/tzmigration/data_generator.rb', line 12 def initialize(out, url: 'https://github.com/tzinfo/tzinfo-data.git') @out = out @url = url @path = File.join(@out, 'repo', 'tzinfo-data') FileUtils.mkdir_p @out repo end |
Instance Attribute Details
#out ⇒ Object (readonly)
Returns the value of attribute out.
10 11 12 |
# File 'lib/a0/tzmigration/data_generator.rb', line 10 def out @out end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/a0/tzmigration/data_generator.rb', line 10 def path @path end |
#timezones ⇒ Object (readonly)
Returns the value of attribute timezones.
10 11 12 |
# File 'lib/a0/tzmigration/data_generator.rb', line 10 def timezones @timezones end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
10 11 12 |
# File 'lib/a0/tzmigration/data_generator.rb', line 10 def url @url end |
#versions ⇒ Object (readonly)
Returns the value of attribute versions.
10 11 12 |
# File 'lib/a0/tzmigration/data_generator.rb', line 10 def versions @versions end |
Instance Method Details
#data_from_transition(transition) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/a0/tzmigration/data_generator/process.rb', line 95 def data_from_transition(transition) offset = transition.offset previous_offset = transition.previous_offset { utc_offset: offset.utc_total_offset, utc_prev_offset: previous_offset.utc_total_offset, utc_timestamp: transition.time.to_i, utc_time: transition.datetime, local_ini_str: transition.local_end_time.strftime("%F %T #{previous_offset.abbreviation}"), local_fin_str: transition.local_start_time.strftime("%F %T #{offset.abbreviation}") } end |
#generate ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/a0/tzmigration/data_generator/process.rb', line 10 def generate load_release_dates generate_versions generate_timezones save_versions save_versions_index save_timezones save_timezones_index save_index end |
#generate_timezones ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/a0/tzmigration/data_generator/process.rb', line 34 def generate_timezones @timezones = {} @versions.each do |version_data| version_data[:timezones].each do |name, timezone_data| @timezones[name] ||= { name: name, versions: {} } @timezones[name][:versions][version_data[:version]] = { tag: version_data[:tag], released_at: version_data[:released_at] }.merge timezone_data end end @timezones = @timezones.to_a.sort_by(&:first).to_h end |
#generate_versions ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/a0/tzmigration/data_generator/process.rb', line 23 def generate_versions = repo..map(&:name) n_proc = Etc.nprocessors || 2 @versions = Parallel.map(, in_processes: n_proc, progress: 'Processing') do |tag| process_tag tag end @versions.sort_by! { |version_data| version_data[:version] } end |
#load_release_dates ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/a0/tzmigration/data_generator/process.rb', line 46 def load_release_dates @released_at = {} File.open(File.join(@path, 'data', 'NEWS')).each_line do |line| next unless line.start_with? 'Release ' release, time = line.split(' - ') version = release.split.last next unless time @released_at[version] = Time.parse(time) end end |
#process(tag) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/a0/tzmigration/data_generator/process.rb', line 69 def process(tag) version = TZInfo::Data::Version::TZDATA timezones = {} = { tag: tag, version: version, released_at: @released_at[version], timezones: timezones } TZInfo::Timezone.all.each do |timezone| process_timezone timezones, timezone.send(:real_timezone) end end |
#process_tag(tag) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/a0/tzmigration/data_generator/process.rb', line 60 def process_tag(tag) repo_use tag tzinfo_unload tzinfo_load process tag end |
#process_timezone(timezones, real_timezone) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/a0/tzmigration/data_generator/process.rb', line 81 def process_timezone(timezones, real_timezone) name = real_timezone.name canonical_name = real_timezone.canonical_identifier if name == canonical_name transitions = real_timezone.send(:info).instance_variable_get(:@transitions)&.map do |transition| data_from_transition(transition) end timezones[name] = { transitions: transitions } else timezones[name] = { alias: canonical_name } end end |
#repo ⇒ Object
8 9 10 11 12 |
# File 'lib/a0/tzmigration/data_generator/git.rb', line 8 def repo return @repo if defined? @repo File.exist?(@path) ? repo_update : repo_clone end |
#repo_clone ⇒ Object
23 24 25 26 27 |
# File 'lib/a0/tzmigration/data_generator/git.rb', line 23 def repo_clone puts "Cloning #{@url} repo at #{@path}" @repo = Git.clone @url, @path end |
#repo_update ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/a0/tzmigration/data_generator/git.rb', line 14 def repo_update puts "Updating #{@url} repo at #{@path}" @repo = Git.open @path @repo.pull @repo end |
#repo_use(tag) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/a0/tzmigration/data_generator/git.rb', line 29 def repo_use(tag) tag_path = File.join(@out, 'repo', tag) lib_path = File.join(tag_path, 'lib') $LOAD_PATH.unshift File.(lib_path) return if File.exist? lib_path FileUtils.rm_rf tag_path FileUtils.copy_entry @path, tag_path tag_repo = Git.open tag_path tag_repo.checkout tag end |
#save_index ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/a0/tzmigration/data_generator/save.rb', line 56 def save_index File.open(File.join(@out, 'index.md'), 'w') do |file| file.write version_head @timezones.keys.sort.map do |timezone_name| file.write timezones_row(timezone_name) end end end |
#save_timezones ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/a0/tzmigration/data_generator/save.rb', line 18 def save_timezones FileUtils.mkdir_p File.join(@out, 'timezones') @timezones.each do |name, timezone_data| dir = name.split('/')[0...-1] FileUtils.mkdir_p File.join(@out, 'timezones', dir) unless dir.empty? File.open(File.join(@out, 'timezones', "#{name}.json"), 'w') do |file| file.write(JSON.generate(timezone_data)) end end end |
#save_timezones_index ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/a0/tzmigration/data_generator/save.rb', line 43 def save_timezones_index FileUtils.mkdir_p File.join(@out, 'timezones') File.open(File.join(@out, 'timezones', '00-index.json'), 'w') do |file| object = @timezones.map do |name, timezone_data| versions = timezone_data[:versions].keys [name, { versions: versions }] end.to_h file.write(JSON.generate(timezones: object)) end end |
#save_versions ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/a0/tzmigration/data_generator/save.rb', line 8 def save_versions FileUtils.mkdir_p File.join(@out, 'versions') @versions.each do |version_data| File.open(File.join(@out, 'versions', "#{version_data[:version]}.json"), 'w') do |file| file.write(JSON.generate(version_data)) end end end |
#save_versions_index ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/a0/tzmigration/data_generator/save.rb', line 31 def save_versions_index FileUtils.mkdir_p File.join(@out, 'versions') File.open(File.join(@out, 'versions', '00-index.json'), 'w') do |file| versions = @versions.map do |version_data| [version_data[:version], { released_at: version_data[:released_at], timezones: version_data[:timezones].keys }] end.to_h file.write(JSON.generate(versions: versions)) end end |
#timezones_row(timezone_name) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/a0/tzmigration/data_generator/save.rb', line 75 def timezones_row(timezone_name) row = @versions.map do |version_data| data = @timezones[timezone_name][:versions][version_data[:version]] if data && data[:alias] "→ #{data[:alias]}" elsif data "#{data[:transitions].count} " end end.join ' | ' "| [#{timezone_name}](timezones/#{timezone_name}.json) | #{row} |\n" end |
#tzinfo_load ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/a0/tzmigration/data_generator.rb', line 22 def tzinfo_load old_verbose = $VERBOSE $VERBOSE = nil require 'tzinfo' require 'tzinfo/data/indexes/countries' require 'tzinfo/data/indexes/timezones' require 'tzinfo/data' $VERBOSE = old_verbose end |
#tzinfo_unload ⇒ Object
34 35 36 37 |
# File 'lib/a0/tzmigration/data_generator.rb', line 34 def tzinfo_unload $LOADED_FEATURES.delete_if { |x| x.include?('tzinfo') } Object.send(:remove_const, :TZInfo) if defined? TZInfo end |
#version_head ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/a0/tzmigration/data_generator/save.rb', line 65 def version_head head_a = @versions.map do |version_data| version = version_data[:version] "[#{version}](versions/#{version}.json)" end.join ' | ' head_b = @versions.map { '---' }.join ' | ' "| | #{head_a} |\n| --- | #{head_b} |\n" end |