Class: TwoPercent::BulkProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/two_percent/bulk_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(operations, correlation_id: nil) ⇒ BulkProcessor

Returns a new instance of BulkProcessor.



5
6
7
8
# File 'lib/two_percent/bulk_processor.rb', line 5

def initialize(operations, correlation_id: nil)
  @operations = operations
  @correlation_id = correlation_id
end

Instance Method Details

#dispatchObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/two_percent/bulk_processor.rb', line 10

def dispatch
  @operations.each do |operation|
    resource_type, id = parse_path(operation[:path])

    # Persist data to two_percent tables first (wrapped in transaction for bulk integrity)
    ActiveRecord::Base.transaction do
      record = persist_bulk_operation(operation[:method], resource_type, id, operation[:data])

      # Publish domain events based on operation
      # Note: DELETE operations don't return a record, but still need to publish events
      if record || operation[:method] == "DELETE"
        publish_domain_event(operation[:method], resource_type, record,
                             id)
      end
    end
  end
end