Class: I18n::Migrations::Migrator
- Inherits:
-
Object
- Object
- I18n::Migrations::Migrator
- Defined in:
- lib/i18n/migrations/migrator.rb
Instance Attribute Summary collapse
-
#allow_translations ⇒ Object
Returns the value of attribute allow_translations.
Instance Method Summary collapse
- #config ⇒ Object
-
#config=(config) ⇒ Object
for testing.
-
#initialize(allow_translations: true) ⇒ Migrator
constructor
A new instance of Migrator.
- #locale_for(name) ⇒ Object
- #migrate(locale_or_all = 'all') ⇒ Object
- #new_locale(new_locale) ⇒ Object
- #new_migration(name) ⇒ Object
- #pull(locale_or_all) ⇒ Object
- #push(locale_or_all, force = false) ⇒ Object
- #rollback(locale_or_all) ⇒ Object
- #validate(locale_or_all) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(allow_translations: true) ⇒ Migrator
Returns a new instance of Migrator.
23 24 25 |
# File 'lib/i18n/migrations/migrator.rb', line 23 def initialize(allow_translations: true) self.allow_translations = allow_translations end |
Instance Attribute Details
#allow_translations ⇒ Object
Returns the value of attribute allow_translations.
21 22 23 |
# File 'lib/i18n/migrations/migrator.rb', line 21 def allow_translations @allow_translations end |
Instance Method Details
#config ⇒ Object
35 36 37 |
# File 'lib/i18n/migrations/migrator.rb', line 35 def config @config ||= Config.new.read! end |
#config=(config) ⇒ Object
for testing
40 41 42 |
# File 'lib/i18n/migrations/migrator.rb', line 40 def config=(config) @config = config end |
#locale_for(name) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/i18n/migrations/migrator.rb', line 27 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
67 68 69 70 71 |
# File 'lib/i18n/migrations/migrator.rb', line 67 def migrate(locale_or_all = 'all') each_locale(locale_or_all) do |locale| locale.migrate! end end |
#new_locale(new_locale) ⇒ Object
96 97 98 |
# File 'lib/i18n/migrations/migrator.rb', line 96 def new_locale(new_locale) locale_for(new_locale).create end |
#new_migration(name) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/i18n/migrations/migrator.rb', line 44 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
81 82 83 84 85 86 |
# File 'lib/i18n/migrations/migrator.rb', line 81 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
88 89 90 91 92 93 94 |
# File 'lib/i18n/migrations/migrator.rb', line 88 def push(locale_or_all, force = false) backend.sync_migrations(new_migrations) each_locale(locale_or_all, concurrency: config.push_concurrency) do |locale| backend.push(locale, force: force) wait end end |
#rollback(locale_or_all) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/i18n/migrations/migrator.rb', line 73 def rollback(locale_or_all) each_locale(locale_or_all) do |locale| locale.update_info do |data, | locale.rollback(data, ) end end end |
#validate(locale_or_all) ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/i18n/migrations/migrator.rb', line 106 def validate(locale_or_all) each_locale(locale_or_all, async: false) do |locale| next if locale.main_locale? locale.update_info do |data, | locale.validate(data, ) end end end |
#version ⇒ Object
100 101 102 103 104 |
# File 'lib/i18n/migrations/migrator.rb', line 100 def version each_locale do |locale| puts "#{locale.name}: #{locale.last_version}" end end |