Module: Dor::Upgradable

Included in:
ContentMetadataDS, Identifiable, Processable, Versionable
Defined in:
lib/dor/models/upgradable.rb

Defined Under Namespace

Classes: Callback

Constant Summary collapse

@@__upgrade_callbacks =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_upgrade_callback(c, v, d, &b) ⇒ Object


28
29
30
# File 'lib/dor/models/upgradable.rb', line 28

def self.add_upgrade_callback c, v, d, &b
  @@__upgrade_callbacks << Callback.new(c, Gem::Version.new(v), d, b)
end

.included(base) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dor/models/upgradable.rb', line 47

def self.included(base)
  base.instance_eval do
    def self.on_upgrade version, desc, &block
      Dor::Upgradable.add_upgrade_callback self, version, desc, &block
    end
    
    Dir[File.join(Dor.root,'dor','migrations',base.name.split(/::/).last.underscore,'*.rb')].each do |migration|
      require migration
    end
  end
end

.run_upgrade_callbacks(obj, event_handler) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dor/models/upgradable.rb', line 32

def self.run_upgrade_callbacks(obj, event_handler)
  relevant = @@__upgrade_callbacks.select { |c| obj.is_a?(c.module) }.sort_by(&:version)
  results = relevant.collect do |c| 
    result = c.block.call(obj)
    if result and event_handler.respond_to?(:add_event)
      event_handler.add_event 'remediation', "#{c.module.name} #{c.version}", c.description
    end
    if result
      Dor.logger.info "Applied remediation '#{c.description}' to '#{obj.pid}'"
    end
    result
  end
  results.any?
end

Instance Method Details

#upgrade!Object


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dor/models/upgradable.rb', line 59

def upgrade!
  results = [Dor::Upgradable.run_upgrade_callbacks(self, self)]
  if self.respond_to?(:datastreams)
    self.datastreams.each_pair do |dsid, ds|
      results << Dor::Upgradable.run_upgrade_callbacks(ds, self) unless ds.new?
    end
  end

  if results.any?
    self.save
  else
    false
  end
end