Class: I18n::Migrations::Locale
- Inherits:
-
Object
- Object
- I18n::Migrations::Locale
- Defined in:
- lib/i18n/migrations/locale.rb
Instance Attribute Summary collapse
-
#migrations ⇒ Object
readonly
Returns the value of attribute migrations.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #assign_complex_key(hash, key, value) ⇒ Object
- #create(limit = nil) ⇒ Object
-
#initialize(name, locales_dir:, main_locale_name:, migrations:, dictionary:) ⇒ Locale
constructor
A new instance of Locale.
- #last_version ⇒ Object
- #main_locale ⇒ Object
- #main_locale? ⇒ Boolean
- #migrate(data, notes) ⇒ Object
- #migrate! ⇒ Object
- #read_data ⇒ Object
- #read_data_and_notes ⇒ Object
- #rollback(data, notes) ⇒ Object
- #update_info {|data, notes| ... } ⇒ Object
- #validate(data, notes) ⇒ Object
- #write_data_and_notes(data, notes) ⇒ Object
- #write_raw_data(filename, data) ⇒ Object
- #write_remote_version(data) ⇒ Object
Constructor Details
#initialize(name, locales_dir:, main_locale_name:, migrations:, dictionary:) ⇒ Locale
Returns a new instance of Locale.
14 15 16 17 |
# File 'lib/i18n/migrations/locale.rb', line 14 def initialize(name, locales_dir:, main_locale_name:, migrations:, dictionary:) @name, @locales_dir, @main_locale_name, @migrations, @dictionary = name, locales_dir, main_locale_name, migrations, dictionary end |
Instance Attribute Details
#migrations ⇒ Object (readonly)
Returns the value of attribute migrations.
12 13 14 |
# File 'lib/i18n/migrations/locale.rb', line 12 def migrations @migrations end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/i18n/migrations/locale.rb', line 11 def name @name end |
Instance Method Details
#assign_complex_key(hash, key, value) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/i18n/migrations/locale.rb', line 143 def assign_complex_key(hash, key, value) if key.length == 0 # should never get here elsif key.length == 1 hash[key[0]] = value else hash[key[0]] ||= {} assign_complex_key(hash[key[0]], key[1..-1], value) end end |
#create(limit = nil) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/i18n/migrations/locale.rb', line 84 def create(limit = nil) new_data, new_notes = {}, {} count = 0 main_data = main_locale.read_data main_data.each do |key, term| if key == 'VERSION' new_data['VERSION'] = main_data['VERSION'] else new_data[key], new_notes[key] = @dictionary.lookup(term, key: key) end print '.'.green break if limit && limit < (count += 1) end puts write_data_and_notes(new_data, new_notes) end |
#last_version ⇒ Object
105 106 107 |
# File 'lib/i18n/migrations/locale.rb', line 105 def last_version read_versions(read_data).last end |
#main_locale ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/i18n/migrations/locale.rb', line 135 def main_locale Locale.new(@main_locale_name, locales_dir: @locales_dir, main_locale_name: @main_locale_name, migrations: @migrations, dictionary: nil) # should not use dictionary on main locale end |
#main_locale? ⇒ Boolean
101 102 103 |
# File 'lib/i18n/migrations/locale.rb', line 101 def main_locale? @name == @main_locale_name end |
#migrate(data, notes) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/i18n/migrations/locale.rb', line 60 def migrate(data, notes) missing_versions = (@migrations.all_versions - read_versions(data)).sort if missing_versions.empty? puts "#{@name}: up-to-date" return end puts "#{@name}: Migrating #{missing_versions.join(', ')}" missing_versions.each do |version| migrate_to_version(data, notes, version, :up) end end |
#migrate! ⇒ Object
54 55 56 57 58 |
# File 'lib/i18n/migrations/locale.rb', line 54 def migrate! update_info do |data, notes| migrate(data, notes) end end |
#read_data ⇒ Object
109 110 111 |
# File 'lib/i18n/migrations/locale.rb', line 109 def read_data read_from_file("#{@name}.yml") end |
#read_data_and_notes ⇒ Object
113 114 115 116 117 |
# File 'lib/i18n/migrations/locale.rb', line 113 def read_data_and_notes data = read_data notes = main_locale? ? {} : read_from_file("../#{@name}_notes.yml") [data, notes] end |
#rollback(data, notes) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/i18n/migrations/locale.rb', line 72 def rollback(data, notes) last_version = read_versions(data).last if last_version == nil puts "#{@name}: no more migrations to roll back" return end puts "#{@name}: Rolling back #{last_version}" raise "Can't find #{last_version}.rb to rollback" unless @migrations.all_versions.include?(last_version) migrate_to_version(data, notes, last_version, :down) end |
#update_info {|data, notes| ... } ⇒ Object
48 49 50 51 52 |
# File 'lib/i18n/migrations/locale.rb', line 48 def update_info data, notes = read_data_and_notes yield data, notes write_data_and_notes(data, notes) end |
#validate(data, notes) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/i18n/migrations/locale.rb', line 19 def validate(data, notes) fix_count, error_count = 0, 0 main_data = main_locale.read_data main_data.each do |key, main_term| old_term = data[key] new_term, errors = @dictionary.fix(main_term, old_term, key: key) if new_term != old_term data[key] = new_term puts "#{"Fix".green} #{key.green}:" puts "#{@main_locale_name}: #{main_term}" puts "#{@name} (old): #{old_term}" puts "#{@name} (new): #{new_term}" puts fix_count += 1 end replace_errors_in_notes(notes, key, errors) if errors.length > 0 puts "Error #{errors.join(', ').red} #{key.yellow}" puts "#{@main_locale_name.bold}: #{main_term}" puts "#{@name.bold}: #{old_term}" puts error_count += 1 end end puts "#{name}: #{fix_count} Fixes" if fix_count > 0 puts "#{name}: #{error_count} Errors" if error_count > 0 end |
#write_data_and_notes(data, notes) ⇒ Object
119 120 121 122 |
# File 'lib/i18n/migrations/locale.rb', line 119 def write_data_and_notes(data, notes) write_data(data) write_to_file("../#{@name}_notes.yml", notes) unless main_locale? end |
#write_raw_data(filename, data) ⇒ Object
124 125 126 127 128 |
# File 'lib/i18n/migrations/locale.rb', line 124 def write_raw_data(filename, data) File.open(File.join(@locales_dir, filename), 'w') do |file| file << data end end |
#write_remote_version(data) ⇒ Object
130 131 132 133 |
# File 'lib/i18n/migrations/locale.rb', line 130 def write_remote_version(data) write_to_file("../#{@name}_remote_version.yml", { 'VERSION' => read_versions(data) }) end |