Class: SimpleEventSourcing::Events::EventStore::RedisEventStore

Inherits:
EventStoreBase
  • Object
show all
Defined in:
lib/simple_event_sourcing/events/event_store/redis/redis_event_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ RedisEventStore

Returns a new instance of RedisEventStore.



8
9
10
# File 'lib/simple_event_sourcing/events/event_store/redis/redis_event_store.rb', line 8

def initialize(client)
  @redis = client
end

Instance Method Details

#commit(event) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/simple_event_sourcing/events/event_store/redis/redis_event_store.rb', line 12

def commit(event)

  stored_event = SimpleEventSourcing::Events::StoredEvent.new(
    aggregate_id: event.aggregate_id,
    occurred_on: Time.now.getlocal("+02:00").to_i,
    event_type: event.class.name,
    event_data: event.to_json
  )

  @redis.rpush(event.aggregate_id, stored_event.to_json )

end

#get_history(aggregate_id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/simple_event_sourcing/events/event_store/redis/redis_event_store.rb', line 25

def get_history(aggregate_id)
  stored_events_json = @redis.lrange( aggregate_id, 0, -1 )
  history = SimpleEventSourcing::AggregateRoot::History.new(aggregate_id)
  stored_events_json.each do |stored_event_json|
    stored_event =  SimpleEventSourcing::Events::StoredEvent.create_from_json stored_event_json
    event = Object.const_get(stored_event.event_type)
    args = JSON.parse(stored_event.event_data)
    args.keys.each do |key|
      args[(key.to_sym rescue key) || key] = args.delete(key)
    end
    recovered_event = event.new(args)
    history << recovered_event
  end
  history
end