Class: TransactionEventStoreMongoid::Transaction

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps::Created::Short
Defined in:
lib/transaction_event_store_mongoid/transaction.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(**attrs) ⇒ Object



37
38
39
# File 'lib/transaction_event_store_mongoid/transaction.rb', line 37

def build(**attrs)
  self.new(**attrs)
end

.for_stream(stream_name) ⇒ Object



41
42
43
# File 'lib/transaction_event_store_mongoid/transaction.rb', line 41

def for_stream(stream_name)
  where(stream: stream_name)
end

.last_snapshot(stream:) ⇒ Object



49
50
51
52
53
# File 'lib/transaction_event_store_mongoid/transaction.rb', line 49

def last_snapshot(stream:)
  tx = last_snapshot_transaction(stream: stream)
  return nil unless tx.present?
  tx.last_snapshot
end

.last_snapshot_transaction(stream:) ⇒ Object



55
56
57
# File 'lib/transaction_event_store_mongoid/transaction.rb', line 55

def last_snapshot_transaction(stream:)
  for_stream(stream).desc(:ts).where(events: { '$elemMatch': { snapshot: true } }).first
end

.last_stream_event(stream:) ⇒ Object



63
64
65
66
67
# File 'lib/transaction_event_store_mongoid/transaction.rb', line 63

def last_stream_event(stream:)
  tx = last_transaction(stream: stream)
  return nil unless tx.present?
  tx.last_event
end

.last_transaction(stream:) ⇒ Object



59
60
61
# File 'lib/transaction_event_store_mongoid/transaction.rb', line 59

def last_transaction(stream:)
  where(stream: stream).desc(:ts).first
end

.with_event(event_id) ⇒ Object



45
46
47
# File 'lib/transaction_event_store_mongoid/transaction.rb', line 45

def with_event(event_id)
  where(events: { '$elemMatch': { event_id: event_id } })
end

Instance Method Details

#last_eventObject



28
29
30
# File 'lib/transaction_event_store_mongoid/transaction.rb', line 28

def last_event
  events.last
end

#last_snapshotObject



32
33
34
# File 'lib/transaction_event_store_mongoid/transaction.rb', line 32

def last_snapshot
  events.reverse.find(&:snapshot)
end