Class: EventStoreClient::InMemory

Inherits:
Object
  • Object
show all
Defined in:
lib/event_store_client/adapters/in_memory.rb

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#event_storeObject (readonly)

Returns the value of attribute event_store.



13
14
15
# File 'lib/event_store_client/adapters/in_memory.rb', line 13

def event_store
  @event_store
end

Instance Method Details

#append_to_stream(stream_name, events, options: {}) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument,Metrics/LineLength



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/event_store_client/adapters/in_memory.rb', line 15

def append_to_stream(stream_name, events, options: {}) # rubocop:disable Lint/UnusedMethodArgument,Metrics/LineLength
  event_store[stream_name] = [] unless event_store.key?(stream_name)
  [events].flatten.each do |event|
    event_store[stream_name].unshift(
      'eventId' => event.id,
      'data' => event.data,
      'eventType' => event.type,
      'metaData' => event.,
      'positionEventNumber' => event_store[stream_name].length
    )
  end
  Dry::Monads::Success(events)
end

#delete_stream(stream_name, options: {}) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



29
30
31
# File 'lib/event_store_client/adapters/in_memory.rb', line 29

def delete_stream(stream_name, options: {}) # rubocop:disable Lint/UnusedMethodArgument
  event_store.delete(stream_name)
end


65
66
67
# File 'lib/event_store_client/adapters/in_memory.rb', line 65

def link_to(stream_name, events, **)
  append_to_stream(stream_name, events)
end

#listen(subscription, options: {}) ⇒ Object



69
70
71
# File 'lib/event_store_client/adapters/in_memory.rb', line 69

def listen(subscription, options: {})
  # TODO: implement method body
end

#read(stream_name, options: {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/event_store_client/adapters/in_memory.rb', line 37

def read(stream_name, options: {})
  start = options[:start] == 'head' ? options[:start] : options[:start].to_i
  direction = options[:direction] || 'forward'
  response =
    if %w[forward forwards].include?(direction)
      read_stream_forward(stream_name, start: start)
    else
      read_stream_backward(stream_name, start: start)
    end

  res = Response.new(response.to_json, 200)

  return [] if res.body.nil? || res.body.empty?
  skip_decryption = options[:skip_decryption] || false
  events = JSON.parse(res.body)['entries'].map do |entry|
    deserialize_event(entry, skip_decryption: skip_decryption)
  end.reverse
  Dry::Monads::Success(events)
end

#read_all_from_stream(stream_name, options: {}) ⇒ Object



57
58
59
# File 'lib/event_store_client/adapters/in_memory.rb', line 57

def read_all_from_stream(stream_name, options: {})
  read(stream_name, options: options)
end

#subscribe_to_stream(subscription) ⇒ Object



61
62
63
# File 'lib/event_store_client/adapters/in_memory.rb', line 61

def subscribe_to_stream(subscription, **)
  # TODO: implement method body
end

#tombstone_stream(stream_name, options: {}) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



33
34
35
# File 'lib/event_store_client/adapters/in_memory.rb', line 33

def tombstone_stream(stream_name, options: {}) # rubocop:disable Lint/UnusedMethodArgument
  event_store.delete(stream_name)
end