Class: JSONAPI::OperationDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/operation_dispatcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(transaction: lambda { |&block| block.yield }, rollback: lambda { }, server_error_callbacks: []) ⇒ OperationDispatcher


4
5
6
7
8
9
10
11
# File 'lib/jsonapi/operation_dispatcher.rb', line 4

def initialize(transaction: lambda { |&block| block.yield },
               rollback: lambda { },
               server_error_callbacks: [])

  @transaction = transaction
  @rollback = rollback
  @server_error_callbacks = server_error_callbacks
end

Instance Method Details

#process(operations) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jsonapi/operation_dispatcher.rb', line 13

def process(operations)
  results = JSONAPI::OperationResults.new

  # Use transactions if more than one operation and if one of the operations can be transactional
  # Even if transactional transactions won't be used unless the derived OperationsProcessor supports them.
  transactional = false
  operations.each do |operation|
    transactional |= operation.transactional?
  end

  transaction(transactional) do
    # Links and meta data global to the set of operations
    operations_meta = {}
    operations_links = {}
    operations.each do |operation|
      results.add_result(process_operation(operation))
      rollback(transactional) if results.has_errors?
    end
    results.meta = operations_meta
    results.links = operations_links
  end
  results
end