Class: RubyEventStore::ROM::Repositories::Events

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

Instance Method Summary collapse

Instance Method Details

#count(specification) ⇒ Object



54
55
56
57
58
# File 'lib/ruby_event_store/rom/repositories/events.rb', line 54

def count(specification)
  query = read_scope(specification)
  query = query.limit(specification.limit) if specification.limit?
  query.count
end

#create_changeset(records) ⇒ Object



7
8
9
# File 'lib/ruby_event_store/rom/repositories/events.rb', line 7

def create_changeset(records)
  events.create_changeset(records)
end

#exist?(event_id) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/ruby_event_store/rom/repositories/events.rb', line 21

def exist?(event_id)
  events.by_event_id(event_id).exist?
end

#find_nonexistent_pks(event_ids) ⇒ Object



15
16
17
18
19
# File 'lib/ruby_event_store/rom/repositories/events.rb', line 15

def find_nonexistent_pks(event_ids)
  return event_ids unless event_ids.any?

  event_ids - events.by_event_id(event_ids).pluck(:event_id)
end

#global_position(event_id) ⇒ Object

Raises:

  • (EventNotFound)


60
61
62
63
64
# File 'lib/ruby_event_store/rom/repositories/events.rb', line 60

def global_position(event_id)
  record = events.by_event_id(event_id).one
  raise EventNotFound.new(event_id) if record.nil?
  record.id - 1
end

#last_stream_event(stream, serializer) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/ruby_event_store/rom/repositories/events.rb', line 25

def last_stream_event(stream, serializer)
  query = stream_entries.ordered(:backward, stream)
  query = query.combine(:event)
  query = query.map_with(:stream_entry_to_serialized_record, auto_struct: false)
  query = query_builder(serializer, query, limit: 1)
  query.first
end

#read(specification, serializer) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby_event_store/rom/repositories/events.rb', line 33

def read(specification, serializer)
  query = read_scope(specification)

  if specification.batched?
    BatchEnumerator.new(
      specification.batch_size,
      specification.limit,
      ->(offset, limit) { query_builder(serializer, query, offset: offset, limit: limit).to_ary }
    ).each
  else
    query = query_builder(serializer, query, limit: (specification.limit if specification.limit?))
    if !specification.start && !specification.stop
      specification.first? || specification.last? ? query.first : query.each
    elsif specification.last?
      query.to_ary.last
    else
      specification.first? ? query.first : query.each
    end
  end
end

#update_changeset(records) ⇒ Object



11
12
13
# File 'lib/ruby_event_store/rom/repositories/events.rb', line 11

def update_changeset(records)
  events.update_changeset(records)
end