class Harold::Strategies::Cancel

Public Class Methods

call(operations) click to toggle source
# File lib/strategies/cancel.rb, line 4
def self.call(operations)
  new(operations).cancel
end
new(operations) click to toggle source
# File lib/strategies/cancel.rb, line 10
def initialize(operations)
  @operations = operations
  @useful_items = useful_items(operations)
end

Public Instance Methods

cancel() click to toggle source
# File lib/strategies/cancel.rb, line 15
def cancel
  @operations.select do |operation|
    @useful_items.include?(operation.id)
  end
end

Private Instance Methods

useful_items(operations) click to toggle source

This lists the operation id's which are considered useful. The result of this method are all the id's of operations which have been added 'or' updated. All items which are added/updated 'and' afterwards deleted will be non-present in this list. If an operation has been updated and afterards deleted, both operations will be returned.

# File lib/strategies/cancel.rb, line 29
def useful_items(operations)
  items = Hash.new { |hash, key| hash[key] = [] }

  operations.each do |operation|
    if operation.type == :delete && items[operation.id].include?(:add)
      items.delete(operation.id)
    else
      items[operation.id] << operation.type
    end
  end

  items.keys
end