Module: Euston::Projections::Idempotence::InstanceMethods

Defined in:
lib/euston-projections/idempotence.rb

Instance Method Summary collapse

Instance Method Details

#if_exists_and_unhandled(document_type, id, headers) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
# File 'lib/euston-projections/idempotence.rb', line 34

def if_exists_and_unhandled document_type, id, headers
  document = document_type.find id

  raise ArgumentError, "Expected to find a #{document_type} with id #{id}, but none exists." if document.nil?

  if_unhandled document, headers do
    yield document
  end
end

#if_unhandled(obj, headers) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/euston-projections/idempotence.rb', line 7

def if_unhandled obj, headers
  return if obj.already_processed_message? headers.id

  yield

  obj.add_to_history headers.id

  unless headers.source_message.nil? || obj.already_processed_message?(headers.source_message[:headers][:id])
    obj.add_to_history headers.source_message[:headers][:id]
  end
end

#if_unhandled_or_new(document_type, id, headers, new_document_attributes = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/euston-projections/idempotence.rb', line 19

def if_unhandled_or_new document_type, id, headers, new_document_attributes = {}
  if id.respond_to? :call
    query = id
  else
    query = ->() { document_type.find(id) }
    new_document_attributes = { _id: id }
  end

  document = query.call || document_type.new(new_document_attributes)

  if_unhandled document, headers do
    yield document
  end
end