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



11
12
13
14
# File 'app/models/aliquot/remover.rb', line 11

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

#process_aliquots(aliquots_to_remove) ⇒ Object



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

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
# File 'app/models/aliquot/remover.rb', line 6

def remove_downstream_aliquots
  # On the target asset of the failed request.
  on_downstream_aliquots(aliquots)
end

#remove_matching_aliquots(aliquots_to_remove) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'app/models/aliquot/remover.rb', line 21

def remove_matching_aliquots(aliquots_to_remove)
  aliquots_to_remove.filter_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
end