Module: Sequent::Core::Helpers::MessageHandler

Included in:
AggregateRoot, BaseCommandHandler, Projector, Workflow
Defined in:
lib/sequent/core/helpers/message_handler.rb

Overview

Creates ability to use DSL like:

class MyProjector < Sequent::Projector

  on MyEvent do |event|
    @foo = event.foo
  end

end

If you extend from Sequent::AggregateRoot, Sequent::Projector, Sequent::Workflow or Sequent::CommandHandler you will get this functionality for free.

It is possible to register multiple handler blocks in the same MessageHandler

class MyProjector < Sequent::Projector

  on MyEvent do |event|
    @foo = event.foo
  end

  on MyEvent, OtherEvent do |event|
    @bar = event.bar
  end

end

The order of which handler block is executed first is not guaranteed.

Defined Under Namespace

Modules: ClassMethods Classes: OnArgumentsValidator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(host_class) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/sequent/core/helpers/message_handler.rb', line 95

def self.included(host_class)
  host_class.extend(ClassMethods)
  host_class.extend(MessageMatchers)
  host_class.extend(AttrMatchers)

  host_class.class_attribute :option_registry, default: MessageHandlerOptionRegistry.new
end

Instance Method Details

#dispatch_message(message, handlers) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/sequent/core/helpers/message_handler.rb', line 108

def dispatch_message(message, handlers)
  handlers.each do |handler|
    if Sequent.logger.debug?
      Sequent.logger.debug("[MessageHandler] Handler #{self.class} handling #{message.class}")
    end
    instance_exec(message, &handler)
  end
end

#handle_message(message) ⇒ Object



103
104
105
106
# File 'lib/sequent/core/helpers/message_handler.rb', line 103

def handle_message(message)
  handlers = self.class.message_router.match_message(message)
  dispatch_message(message, handlers) unless handlers.empty?
end