Module: Aliquot::Remover

Included in:
Receptacle
Defined in:
app/models/aliquot/remover.rb

Overview

Provides behaviour to walk downstream and remove aliquots from any downstream assets in the event of a retrospective fail.

Instance Method Summary collapse

Instance Method Details

#on_downstream_aliquots(aliquots_to_remove) ⇒ Object


13
14
15
16
17
18
19
20
# File 'app/models/aliquot/remover.rb', line 13

def on_downstream_aliquots(aliquots_to_remove)
  transfer_requests_as_source.each do |request|
    request.target_asset.process_aliquots(aliquots_to_remove)
  end
  requests_as_source.with_target.each do |request|
    request.target_asset.process_aliquots(aliquots_to_remove)
  end
end

#process_aliquots(aliquots_to_remove) ⇒ Object


22
23
24
25
# File 'app/models/aliquot/remover.rb', line 22

def process_aliquots(aliquots_to_remove)
  new_aliquots = remove_matching_aliquots(aliquots_to_remove)
  on_downstream_aliquots(new_aliquots)
end

#remove_downstream_aliquotsObject


6
7
8
9
10
11
# File 'app/models/aliquot/remover.rb', line 6

def remove_downstream_aliquots
  # On the target asset of the failed request.
  ActiveRecord::Base.transaction do
    on_downstream_aliquots(aliquots)
  end
end

#remove_matching_aliquots(aliquots_to_remove) ⇒ Object


27
28
29
30
31
32
33
34
35
# File 'app/models/aliquot/remover.rb', line 27

def remove_matching_aliquots(aliquots_to_remove)
  aliquots_to_remove.map do |aliquot_to_remove|
    to_remove = aliquots.select { |aliquot| aliquot.matches?(aliquot_to_remove) }
    raise "Duplicate aliquots detected in asset #{display_name}." if to_remove.count > 1
    next unless to_remove.count == 1

    to_remove.first.tap(&:destroy)
  end.compact
end