Class: Dry::Facts::EventStore::InMemoryFactStore

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/facts/event_store.rb

Instance Method Summary collapse

Constructor Details

#initializeInMemoryFactStore

Returns a new instance of InMemoryFactStore.



6
7
8
# File 'lib/dry/facts/event_store.rb', line 6

def initialize
  @serialized_events = []
end

Instance Method Details

#aggregate_by_id(klass, id) ⇒ Object



14
15
16
17
# File 'lib/dry/facts/event_store.rb', line 14

def aggregate_by_id(klass, id)
  get_events_by_aggregate_id(id)
  .yield_self {|events| klass.build_one_from_events(events)}
end

#aggregate_from_event(klass, event) ⇒ Object



29
30
31
32
# File 'lib/dry/facts/event_store.rb', line 29

def aggregate_from_event(klass, event)
  get_events_by_aggregate_id(event.to_h[:metadata][:aggregate_id])
  .yield_self {|events| klass.build_one_from_events(events)}
end

#aggregates_by_ids(klass, ids) ⇒ Object



19
20
21
22
# File 'lib/dry/facts/event_store.rb', line 19

def aggregates_by_ids(klass, ids)
  get_events_by_aggregate_ids(ids)
  .yield_self {|events| klass.build_all_from_events(events)}
end

#all_aggregates_of(klass) ⇒ Object



24
25
26
27
# File 'lib/dry/facts/event_store.rb', line 24

def all_aggregates_of(klass)
  get_events_by_types(klass.event_types)
  .yield_self {|events| klass.build_all_from_events(events)}
end

#commit(event) ⇒ Object

commit(event)



35
36
37
38
39
# File 'lib/dry/facts/event_store.rb', line 35

def commit(event)
  # validate & fail
  @serialized_events << event.to_h
  event
end

#delete_allObject



10
11
12
# File 'lib/dry/facts/event_store.rb', line 10

def delete_all
  @serialized_events = []
end

#deserialize_event(serialized_event) ⇒ Object



78
79
80
81
82
# File 'lib/dry/facts/event_store.rb', line 78

def deserialize_event(serialized_event)
  Event
  .from_h(serialized_event)
  .freeze
end

#get_event_by_id(fact_id) ⇒ Object

get_event_by_id(42)



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dry/facts/event_store.rb', line 42

def get_event_by_id(fact_id)
  # fail if event_id nil?
  # fail if no events found?
  # fail if many events found?
  @serialized_events
  .find_all   {|f|  fact_id == (f && f[:metadata] && f[:metadata][:id])}
  .yield_self {|it| (0 == it.length) ? fail("No event found") : it }
  .yield_self {|it| (1 <  it.length) ? fail("Too may events") : it }
  .first
  .yield_self {|it| deserialize_event(it)}
end

#get_events_by_aggregate_id(value) ⇒ Object



54
55
56
57
58
# File 'lib/dry/facts/event_store.rb', line 54

def get_events_by_aggregate_id(value)
  @serialized_events
  .find_all   {|e|  value == e[:metadata][:aggregate_id]}
  .map        {|it| deserialize_event(it)}
end

#get_events_by_aggregate_ids(value) ⇒ Object



60
61
62
63
64
# File 'lib/dry/facts/event_store.rb', line 60

def get_events_by_aggregate_ids(value)
  @serialized_events
  .find_all   {|e|  [value].flatten.include? e[:metadata][:aggregate_id]}
  .map        {|it| deserialize_event(it)}
end

#get_events_by_types(types) ⇒ Object



67
68
69
70
71
# File 'lib/dry/facts/event_store.rb', line 67

def get_events_by_types(types)
  @serialized_events
  .find_all   {|f| [types].flatten.include? f[:metadata][:type][:name]}
  .map        {|it| deserialize_event(it)}
end

#serialize_event(fact) ⇒ Object



73
74
75
76
# File 'lib/dry/facts/event_store.rb', line 73

def serialize_event(fact)
  fact
  .to_h
end