Class: TransactionEventStoreMongoid::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/transaction_event_store_mongoid/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter: ::TransactionEventStoreMongoid::Transaction, locker: ::TransactionEventStoreMongoid::Locker.new) ⇒ Repository

Returns a new instance of Repository.



5
6
7
8
# File 'lib/transaction_event_store_mongoid/repository.rb', line 5

def initialize(adapter: ::TransactionEventStoreMongoid::Transaction, locker: ::TransactionEventStoreMongoid::Locker.new)
  @adapter = adapter
  @locker = locker
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



3
4
5
# File 'lib/transaction_event_store_mongoid/repository.rb', line 3

def adapter
  @adapter
end

#lockerObject (readonly)

Returns the value of attribute locker.



3
4
5
# File 'lib/transaction_event_store_mongoid/repository.rb', line 3

def locker
  @locker
end

Instance Method Details

#create(event, stream_name) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/transaction_event_store_mongoid/repository.rb', line 25

def create(event, stream_name)
  if in_transaction?
    assert_transaction_matches_stream(stream_name)
    transaction_object.events.build build_event_model(event)
  else
    create_transaction([event], stream_name)
  end
  event
end

#create_snapshot(snapshot, stream_name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/transaction_event_store_mongoid/repository.rb', line 35

def create_snapshot(snapshot, stream_name)
  if in_transaction?
    assert_transaction_matches_stream(stream_name)
    transaction_object.events.build build_snapshot_model(snapshot)
  else
    adapter.create(
      stream: stream_name,
      events: [build_snapshot_model(snapshot)],
    )
  end
end

#create_transaction(events, stream_name) ⇒ Object



47
48
49
50
51
52
# File 'lib/transaction_event_store_mongoid/repository.rb', line 47

def create_transaction(events, stream_name)
  adapter.create(
    stream: stream_name,
    events: events.map(&method(:build_event_model)),
  )
end

#delete_stream(stream_name) ⇒ Object



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

def delete_stream(stream_name)
  condition = {stream: stream_name}
  adapter.destroy_all condition
end

#has_event?(event_id) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_event?(event_id)
  adapter.with_event(event_id).exists?
end

#last_stream_event(stream_name) ⇒ Object



67
68
69
# File 'lib/transaction_event_store_mongoid/repository.rb', line 67

def last_stream_event(stream_name)
  build_event_entity(adapter.last_stream_event(stream: stream_name))
end

#last_stream_snapshot(stream_name) ⇒ Object



63
64
65
# File 'lib/transaction_event_store_mongoid/repository.rb', line 63

def last_stream_snapshot(stream_name)
  build_event_entity(adapter.last_snapshot(stream: stream_name))
end

#read_all_streams_backward(start_event_id, count) ⇒ Object



91
92
93
# File 'lib/transaction_event_store_mongoid/repository.rb', line 91

def read_all_streams_backward(start_event_id, count)
  read_backwards(adapter, start_event_id, count)
end

#read_all_streams_forward(start_event_id, count) ⇒ Object



87
88
89
# File 'lib/transaction_event_store_mongoid/repository.rb', line 87

def read_all_streams_forward(start_event_id, count)
  read_forwards(adapter, start_event_id, count)
end

#read_events_backward(stream_name, start_event_id, count) ⇒ Object



75
76
77
# File 'lib/transaction_event_store_mongoid/repository.rb', line 75

def read_events_backward(stream_name, start_event_id, count)
  read_backwards(adapter.for_stream(stream_name), start_event_id, count)
end

#read_events_forward(stream_name, start_event_id, count) ⇒ Object



71
72
73
# File 'lib/transaction_event_store_mongoid/repository.rb', line 71

def read_events_forward(stream_name, start_event_id, count)
  read_forwards(adapter.for_stream(stream_name), start_event_id, count)
end

#read_stream_events_backward(stream_name) ⇒ Object



83
84
85
# File 'lib/transaction_event_store_mongoid/repository.rb', line 83

def read_stream_events_backward(stream_name)
  read_backwards(adapter.where(stream: stream_name), :head)
end

#read_stream_events_forward(stream_name) ⇒ Object



79
80
81
# File 'lib/transaction_event_store_mongoid/repository.rb', line 79

def read_stream_events_forward(stream_name)
  read_forwards(adapter.where(stream: stream_name), :head)
end

#transaction(stream_name) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/transaction_event_store_mongoid/repository.rb', line 16

def transaction(stream_name)
  raise "Already in transaction" if in_transaction?
  start_transaction(stream_name)
  yield
  commit_transaction
  ensure
    set_transaction_object(nil)
end

#with_lock(stream_name, &block) ⇒ Object



10
11
12
13
14
# File 'lib/transaction_event_store_mongoid/repository.rb', line 10

def with_lock(stream_name, &block)
  locker.with_lock(stream_name) do
    transaction(stream_name, &block)
  end
end