Class: Hydra::Migrate::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/hydra/migrate/dispatcher.rb

Instance Method Summary collapse

Instance Method Details

#can_migrate?(object, constraints = {}) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/hydra/migrate/dispatcher.rb', line 43

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



27
28
29
30
# File 'lib/hydra/migrate/dispatcher.rb', line 27

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



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hydra/migrate/dispatcher.rb', line 15

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



47
48
49
50
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 47

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]
      unless opts[:dry_run]
        unless object.save 
          raise %{Cannot save #{object.pid}:\n#{object.errors.to_a.join("\n")}}
        end
      end
    end
    object
  }
end

#migrations_for(target, constraints = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/hydra/migrate/dispatcher.rb', line 32

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



11
12
13
# File 'lib/hydra/migrate/dispatcher.rb', line 11

def reset!
  @migrations ||= Hash.new { |h,k| h[k] = [] }
end