class Dry::Facts::EventStore::InMemoryFactStore

Public Class Methods

new() click to toggle source
# File lib/dry/facts/event_store.rb, line 6
def initialize
  @serialized_events = []
end

Public Instance Methods

aggregate_by_id(klass, id) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

commit(event)

# File lib/dry/facts/event_store.rb, line 35
def commit(event)
  # validate & fail
  @serialized_events << event.to_h
  event
end
delete_all() click to toggle source
# File lib/dry/facts/event_store.rb, line 10
def delete_all
  @serialized_events = []
end
deserialize_event(serialized_event) click to toggle source
# 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) click to toggle source

get_event_by_id(42)

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/dry/facts/event_store.rb, line 73
def serialize_event(fact)
  fact
  .to_h
end