Class: Hydra::Migrate::Dispatcher
- Inherits:
-
Object
- Object
- Hydra::Migrate::Dispatcher
- Defined in:
- lib/hydra/migrate/dispatcher.rb
Class Method Summary collapse
Instance Method Summary collapse
- #can_migrate?(object, constraints = {}) ⇒ Boolean
- #define_migration(signature = {}, block) ⇒ Object
-
#initialize(path = nil) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #load_migrations(path) ⇒ Object
- #migrate!(*args) ⇒ Object
- #migrations_for(target, constraints = {}) ⇒ Object
- #reset! ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ Dispatcher
Returns a new instance of Dispatcher.
6 7 8 |
# File 'lib/hydra/migrate/dispatcher.rb', line 6 def initialize(path=nil) self.load_migrations(path) unless path.nil? end |
Class Method Details
.migrate_all!(*args, &block) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/hydra/migrate/dispatcher.rb', line 70 def self.migrate_all!(*args, &block) opts = {path: '.'} opts.merge!(args.pop) if args.last.is_a?(Hash) dispatcher = self.new(opts[:path]) models = args models << ActiveFedora::Base if models.empty? models.flatten.each do |klass| klass.find_each({},{:cast=>true}) do |obj| while dispatcher.can_migrate? obj and (opts[:to].nil? or obj.current_migration != opts[:to]) dispatcher.migrate!(obj, &block) end end end end |
Instance Method Details
#can_migrate?(object, constraints = {}) ⇒ Boolean
47 48 49 |
# File 'lib/hydra/migrate/dispatcher.rb', line 47 def can_migrate?(object, constraints={}) object.is_a?(Hydra::ModelMixins::Migratable) and not migrations_for(object, {:from=>object.current_migration}.merge(constraints)).empty? end |
#define_migration(signature = {}, block) ⇒ Object
31 32 33 34 |
# File 'lib/hydra/migrate/dispatcher.rb', line 31 def define_migration(signature={}, block) memo = { :from=>signature[:from].to_s, :to=>signature[:to].to_s, :block=>block } self.migrations[signature[:for]] << memo unless self.migrations[signature[:for]].include?(memo) end |
#load_migrations(path) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/hydra/migrate/dispatcher.rb', line 19 def load_migrations(path) result = [] Dir[File.join(path,'**','*.rb')].each { |migration_file| existing_migrations = Hydra::Migrate::Migration.descendants load(migration_file) new_migrations = Hydra::Migrate::Migration.descendants - existing_migrations new_migrations.each { |klass| klass.new(self) } result = new_migrations } result end |
#migrate!(*args) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/hydra/migrate/dispatcher.rb', line 51 def migrate!(*args) opts = args.last.is_a?(Hash) ? args.pop : {} objects=args.flatten objects.each { |object| raise "Not a migratable object: #{object.inspect}" unless object.is_a?(Hydra::ModelMixins::Migratable) } objects.collect { |object| migrations_for(object, :from=>object.current_migration, :to=>opts[:to]).each do |migration| yield(object,migration,self) if block_given? migration[:block].call(object, migration[:to], self) object.migrationInfo.migrate(migration[:to]) object.current_migration = migration[:to] object.save(:validate=>false) unless opts[:dry_run] end object } end |
#migrations_for(target, constraints = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/hydra/migrate/dispatcher.rb', line 36 def migrations_for(target, constraints={}) raise "Not a migratable object: #{target.inspect}" unless target.is_a?(Hydra::ModelMixins::Migratable) if self.migrations.has_key?(target.class) migrations[target.class].select { |v| v[:from].to_s == constraints[:from].to_s and (constraints[:to].nil? or v[:to].to_s == constraints[:to].to_s) } else return [] end end |
#reset! ⇒ Object
15 16 17 |
# File 'lib/hydra/migrate/dispatcher.rb', line 15 def reset! @migrations ||= Hash.new { |h,k| h[k] = [] } end |