class Harold::Strategies::Update

Public Class Methods

new(scope) click to toggle source
# File lib/strategies/update.rb, line 4
def initialize(scope)
  @scope = scope
end

Public Instance Methods

call(operations) click to toggle source
# File lib/strategies/update.rb, line 8
def call(operations)
  grouped = operations
            .select(&method(:scope))
            .group_by(&method(:scope))

  grouped.each_value do |ops|
    next unless updateable?(ops)

    operations << update_operation(ops.last)
    ops.each { |o| operations.delete(o) }
  end

  operations
end

Private Instance Methods

scope(operation) click to toggle source
# File lib/strategies/update.rb, line 30
def scope(operation)
  operation.payload[@scope]
end
update_operation(operation) click to toggle source
# File lib/strategies/update.rb, line 34
def update_operation(operation)
  Operation.new(
    operation.id,
    :update,
    operation.payload,
    operation.attributes
  )
end
updateable?(ops) click to toggle source
# File lib/strategies/update.rb, line 25
def updateable?(ops)
  types = ops.map(&:type).uniq.sort
  types == %i[add delete]
end