Class: EventSourcing::Aggregate::Actor

Inherits:
Object
  • Object
show all
Defined in:
lib/event_sourcing/aggregate/actor.rb

Class Method Summary collapse

Class Method Details

.for(aggregate) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/event_sourcing/aggregate/actor.rb', line 6

def self.for(aggregate)
  Class.new(Concurrent::Actor::RestartingContext) do
    define_method :initialize do |event_bus, event_stream|
      @aggregate = aggregate.new(event_stream)
      @event_bus = event_bus
      @event_stream = event_stream
    end

    #FIXME: this is doing too many things

    def on_message(message) # Format is [:method, arg1, arg2]
      if @aggregate.respond_to?(message.first)
        events = @aggregate.send(*message) # FIXME: what happens if events is empty or falsy?
        @event_stream.append(events) # FIXME: Event Stream is now stale
        @event_bus.publish(events) 
        # FIXME: spec what happens if event hasn't been stored for some reason
        @aggregate._apply(events)
      end
    end
  end
end