Class: Curator::Migrator
- Inherits:
-
Object
- Object
- Curator::Migrator
- Defined in:
- lib/curator/migrator.rb
Instance Method Summary collapse
- #_all_migrations ⇒ Object
- #_applicable_migrations(current_version) ⇒ Object
-
#initialize(collection_name) ⇒ Migrator
constructor
A new instance of Migrator.
- #migrate(attributes) ⇒ Object
Constructor Details
#initialize(collection_name) ⇒ Migrator
Returns a new instance of Migrator.
3 4 5 6 |
# File 'lib/curator/migrator.rb', line 3 def initialize(collection_name) @collection_name = collection_name @applicable_migrations = {} end |
Instance Method Details
#_all_migrations ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/curator/migrator.rb', line 19 def _all_migrations files = Dir.glob("#{File.join(Curator.config.migrations_path, @collection_name)}/*.rb") files.map do |file| load file migration_version, name = file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first klass = name.camelize.constantize klass.new(migration_version.to_i) end end |
#_applicable_migrations(current_version) ⇒ Object
15 16 17 |
# File 'lib/curator/migrator.rb', line 15 def _applicable_migrations(current_version) @applicable_migrations[current_version] ||= _all_migrations.select { |migration| migration.version > current_version }.sort_by(&:version) end |
#migrate(attributes) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/curator/migrator.rb', line 8 def migrate(attributes) migrations = _applicable_migrations(attributes["version"].to_i) migrations.inject(attributes) do |migrated_attributes, migration| migration.migrate(migrated_attributes).merge("version" => migration.version) end end |