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)


39
40
41
42
43
44
45
46
47
# File 'lib/euston-projections/idempotence.rb', line 39

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) ⇒ Object



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

def if_unhandled_or_new document_type, id, headers
  if id.respond_to? :call
    query = id
    attributes = if document_type.fields['_id'].class.to_s =~ /String/
      { _id: Uuid.generate }
    else
      {}
    end
  else
    query = ->() { document_type.find(id) }
    attributes = { _id: id }
  end

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

  if_unhandled document, headers do
    yield document
  end
end