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.



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

def event_store
  @event_store
end

Instance Method Details

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

rubocop:disable Lint/UnusedMethodArgument,Metrics/LineLength



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

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()
end

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

rubocop:disable Lint/UnusedMethodArgument



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

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


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

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

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



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

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

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



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

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



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

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

#subscribe_to_stream(subscription) ⇒ Object



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

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

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

rubocop:disable Lint/UnusedMethodArgument



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

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