Class: I18n::Migrations::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/migrations/locale.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, locales_dir:, main_locale_name:, migrations:, dictionary:) ⇒ Locale

Returns a new instance of Locale.



15
16
17
18
# File 'lib/i18n/migrations/locale.rb', line 15

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

#migrationsObject (readonly)

Returns the value of attribute migrations.



13
14
15
# File 'lib/i18n/migrations/locale.rb', line 13

def migrations
  @migrations
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/i18n/migrations/locale.rb', line 12

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/i18n/migrations/locale.rb', line 85

def create(limit = nil)
  new_data,  = {}, Metadata.new
  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], errors = @dictionary.lookup(term, key: key)
      [key].errors = errors
    end
    print '.'.green
    break if limit && limit < (count += 1)
  end
  puts
  (new_data, )
end

#data_fileObject



154
155
156
# File 'lib/i18n/migrations/locale.rb', line 154

def data_file
  file("#{name}.yml")
end

#last_versionObject



107
108
109
# File 'lib/i18n/migrations/locale.rb', line 107

def last_version
  read_versions(read_data).last
end

#main_localeObject



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

Returns:

  • (Boolean)


103
104
105
# File 'lib/i18n/migrations/locale.rb', line 103

def main_locale?
  @name == @main_locale_name
end

#metadata_fileObject



158
159
160
# File 'lib/i18n/migrations/locale.rb', line 158

def 
  file("../#{name}_metadata.yml")
end

#migrate(data, metadata) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/i18n/migrations/locale.rb', line 61

def migrate(data, )
  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, , version, :up)
  end
end

#migrate!Object



55
56
57
58
59
# File 'lib/i18n/migrations/locale.rb', line 55

def migrate!
  update_info do |data, |
    migrate(data, )
  end
end

#read_data(parse: true) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/i18n/migrations/locale.rb', line 111

def read_data(parse: true)
  contents = data_file.read
  return contents unless parse

  hash = {}
  add_to_hash(hash, parse_yaml(contents)[@name.to_s])
  hash
end

#read_data_and_metadata(parse: true) ⇒ Object



120
121
122
123
124
# File 'lib/i18n/migrations/locale.rb', line 120

def (parse: true)
  data = read_data(parse: parse)
   = (parse: parse)
  [data, ]
end

#remote_version_fileObject



162
163
164
# File 'lib/i18n/migrations/locale.rb', line 162

def remote_version_file
  file("../#{@name}_remote_version.yml")
end

#rollback(data, metadata) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/i18n/migrations/locale.rb', line 73

def rollback(data, )
  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, , last_version, :down)
end

#update_info {|data, metadata| ... } ⇒ Object

Yields:

  • (data, metadata)


49
50
51
52
53
# File 'lib/i18n/migrations/locale.rb', line 49

def update_info
  data,  = 
  yield data, 
  (data, )
end

#validate(data, metadata) ⇒ Object



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
47
# File 'lib/i18n/migrations/locale.rb', line 20

def validate(data, )
  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
    [key].errors = 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_metadata(data, metadata) ⇒ Object



126
127
128
129
# File 'lib/i18n/migrations/locale.rb', line 126

def (data, )
  write_data(data)
  ()
end

#write_remote_version(data) ⇒ Object



131
132
133
# File 'lib/i18n/migrations/locale.rb', line 131

def write_remote_version(data)
  remote_version_file.write({ 'VERSION' => read_versions(data) }.to_yaml)
end