Class: I18n::Migrations::Migrator

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

Instance Method Summary collapse

Instance Method Details

#configObject



27
28
29
# File 'lib/i18n/migrations/migrator.rb', line 27

def config
  @config ||= Config.new.read!
end

#config=(config) ⇒ Object

for testing



32
33
34
# File 'lib/i18n/migrations/migrator.rb', line 32

def config=(config)
  @config = config
end

#locale_for(name) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/i18n/migrations/migrator.rb', line 19

def locale_for(name)
  Locale.new(name,
             locales_dir: config.locales_dir,
             main_locale_name: config.main_locale,
             migrations: new_migrations,
             dictionary: new_dictionary(name))
end

#migrate(locale_or_all = 'all') ⇒ Object



59
60
61
62
63
# File 'lib/i18n/migrations/migrator.rb', line 59

def migrate(locale_or_all = 'all')
  each_locale(locale_or_all) do |locale|
    locale.migrate!
  end
end

#new_locale(new_locale) ⇒ Object



87
88
89
# File 'lib/i18n/migrations/migrator.rb', line 87

def new_locale(new_locale)
  locale_for(new_locale).create
end

#new_migration(name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/i18n/migrations/migrator.rb', line 36

def new_migration(name)
  name = name.parameterize(separator: '_')
  time = Time.now.strftime('%Y%m%d%H%M')
  file_name = "#{time}_#{name.downcase.gsub(' ', '_')}.rb"
  unless Dir.exist?(config.migration_dir)
    puts "Creating migration directory #{config.migration_dir} because it didn't exist."
    FileUtils.mkdir_p(config.migration_dir)
  end
  full_file_name = File.join(config.migration_dir, file_name)
  File.open(full_file_name, 'w') do |f|
    f << <<-CONTENTS
require 'i18n-migrations'

class #{name.camelcase}#{time} < I18n::Migrations::Migration
  def change
    # add('foo.bar', 'The foo of the bar')
  end
end
    CONTENTS
  end
  puts "Wrote new migration to #{full_file_name}"
end

#pull(locale_or_all) ⇒ Object



73
74
75
76
77
78
# File 'lib/i18n/migrations/migrator.rb', line 73

def pull(locale_or_all)
  each_locale(locale_or_all) do |locale|
    next if locale.main_locale?
    backend.pull(locale)
  end
end

#push(locale_or_all, force = false) ⇒ Object



80
81
82
83
84
85
# File 'lib/i18n/migrations/migrator.rb', line 80

def push(locale_or_all, force = false)
  each_locale(locale_or_all, concurrency: config.push_concurrency) do |locale|
    backend.push(locale, force)
    wait
  end
end

#rollback(locale_or_all) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/i18n/migrations/migrator.rb', line 65

def rollback(locale_or_all)
  each_locale(locale_or_all) do |locale|
    locale.update_info do |data, notes|
      locale.rollback(data, notes)
    end
  end
end

#validate(locale_or_all) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/i18n/migrations/migrator.rb', line 97

def validate(locale_or_all)
  each_locale(locale_or_all, async: false) do |locale|
    next if locale.main_locale?
    locale.update_info do |data, notes|
      locale.validate(data, notes)
    end
  end
end

#versionObject



91
92
93
94
95
# File 'lib/i18n/migrations/migrator.rb', line 91

def version
  each_locale do |locale|
    puts "#{locale.name}: #{locale.last_version}"
  end
end