Class: JSONAPI::OperationsProcessor

Inherits:
Object
  • Object
show all
Includes:
Callbacks
Defined in:
lib/jsonapi/operations_processor.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

included

Class Method Details

.operations_processor_for(operations_processor) ⇒ Object



21
22
23
24
# File 'lib/jsonapi/operations_processor.rb', line 21

def operations_processor_for(operations_processor)
  operations_processor_class_name = "#{operations_processor.to_s.camelize}OperationsProcessor"
  operations_processor_class_name.safe_constantize
end

Instance Method Details

#process(request) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jsonapi/operations_processor.rb', line 27

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

  # 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
  if @operations.length > 1
    @operations.each do |operation|
      @transactional = @transactional | operation.transactional
    end
  end

  run_callbacks :operations do
    transaction do
      # Links and meta data global to the set of operations
      @operations_meta = {}
      @operations_links = {}
      @operations.each do |operation|
        @operation = operation
        # Links and meta data for each operation
        @operation_meta = {}
        @operation_links = {}
        run_callbacks :operation do
          @result = nil
          run_callbacks @operation.class.name.demodulize.underscore.to_sym do
            @result = process_operation(@operation)
          end
          @result.meta.merge!(@operation_meta)
          @result.links.merge!(@operation_links)
          @results.add_result(@result)
          if @results.has_errors?
            rollback
          end
        end
      end
      @results.meta = @operations_meta
      @results.links = @operations_links
    end
  end
  @results
end