class Sequent::Core::Helpers::MessageRouter
Attributes
instanceof_routes[R]
routes[R]
Public Class Methods
new()
click to toggle source
# File lib/sequent/core/helpers/message_router.rb, line 12 def initialize clear_routes end
Public Instance Methods
clear_routes()
click to toggle source
Removes all routes from the router.
# File lib/sequent/core/helpers/message_router.rb, line 54 def clear_routes @instanceof_routes = Hash.new { |h, k| h[k] = Set.new } @routes = Hash.new { |h, k| h[k] = Set.new } end
match_message(message)
click to toggle source
Returns a set of handlers that match the given message, or an empty set when none match.
# File lib/sequent/core/helpers/message_router.rb, line 35 def match_message(message) result = Set.new result.merge(@instanceof_routes[message.class]) @routes.each do |matcher, handlers| result.merge(handlers) if matcher.matches_message?(message) end result end
matches_message?(message)
click to toggle source
Returns true when there is at least one handler for the given message, or false otherwise.
# File lib/sequent/core/helpers/message_router.rb, line 47 def matches_message?(message) match_message(message).any? end
register_matchers(*matchers, handler)
click to toggle source
Registers a handler for the given matchers.
A matcher must implement matches_message?(message)
and return a truthy value when it matches, or a falsey value otherwise.
# File lib/sequent/core/helpers/message_router.rb, line 22 def register_matchers(*matchers, handler) matchers.each do |matcher| if matcher.is_a?(MessageMatchers::InstanceOf) @instanceof_routes[matcher.expected_class] << handler else @routes[matcher] << handler end end end