module Sequent::Core::Helpers::MessageHandler

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.

Public Class Methods

included(host_class) click to toggle source
# 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

Public Instance Methods

dispatch_message(message, handlers) click to toggle source
# 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) click to toggle source
# 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