Class: RubyEventStore::ROM::Repositories::StreamEntries

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_event_store/rom/repositories/stream_entries.rb

Constant Summary collapse

POSITION_SHIFT =
1

Instance Method Summary collapse

Instance Method Details

#create_changeset(event_ids, stream, resolved_version) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ruby_event_store/rom/repositories/stream_entries.rb', line 9

def create_changeset(event_ids, stream, resolved_version)
  tuples = []

  event_ids.each_with_index do |event_id, index|
    unless stream.global?
      tuples << {
        stream: stream.name,
        position: resolved_version && resolved_version + index + POSITION_SHIFT,
        event_id: event_id
      }
    end
  end

  stream_entries.create_changeset(tuples)
end

#delete(stream) ⇒ Object



25
26
27
# File 'lib/ruby_event_store/rom/repositories/stream_entries.rb', line 25

def delete(stream)
  stream_entries.by_stream(stream).command(:delete).call
end

#event_in_stream?(event_id, stream) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/ruby_event_store/rom/repositories/stream_entries.rb', line 46

def event_in_stream?(event_id, stream)
  stream_entries.by_stream(stream).by_event_id(event_id).exist?
end

#position_in_stream(event_id, stream) ⇒ Object

Raises:

  • (EventNotFoundInStream)


40
41
42
43
44
# File 'lib/ruby_event_store/rom/repositories/stream_entries.rb', line 40

def position_in_stream(event_id, stream)
  record = stream_entries.by_stream(stream).by_event_id(event_id).one
  raise EventNotFoundInStream if record.nil?
  record.position
end

#resolve_version(stream, expected_version) ⇒ Object



29
30
31
32
33
34
# File 'lib/ruby_event_store/rom/repositories/stream_entries.rb', line 29

def resolve_version(stream, expected_version)
  expected_version.resolve_for(
    stream,
    lambda { |_stream| (stream_entries.max_position(stream) || {})[:position] }
  )
end

#streams_of(event_id) ⇒ Object



36
37
38
# File 'lib/ruby_event_store/rom/repositories/stream_entries.rb', line 36

def streams_of(event_id)
  stream_entries.by_event_id(event_id).map { |e| e[:stream] }
end