Module: Aggregates::MessageProcessor

Included in:
AggregateRoot, CommandProcessor, EventProcessor
Defined in:
lib/aggregates/message_processor.rb

Overview

MessageProcessor is a set of helper methods for routing messages to handlers defined at the class level for DomainMessages.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(host_class) ⇒ Object



26
27
28
# File 'lib/aggregates/message_processor.rb', line 26

def self.included(host_class)
  host_class.extend(ClassMethods)
end

Instance Method Details

#handle_message(message) ⇒ Object



47
48
49
# File 'lib/aggregates/message_processor.rb', line 47

def handle_message(message)
  invoke_handlers(message)
end

#invoke_handlers(message, *additional_args) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/aggregates/message_processor.rb', line 39

def invoke_handlers(message, *additional_args)
  results = []
  with_message_handlers(message) do |handler|
    results << instance_exec(message, *additional_args, &handler)
  end
  results
end

#with_message_handlers(message, &block) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/aggregates/message_processor.rb', line 30

def with_message_handlers(message, &block)
  search_class = message.class
  while search_class != DomainMessage
    handlers = self.class.message_mapping[search_class]
    handlers&.each(&block)
    search_class = search_class.superclass
  end
end