Class: Sequent::Core::Helpers::MessageRouter
- Inherits:
-
Object
- Object
- Sequent::Core::Helpers::MessageRouter
- Defined in:
- lib/sequent/core/helpers/message_router.rb
Instance Attribute Summary collapse
-
#instanceof_routes ⇒ Object
readonly
Returns the value of attribute instanceof_routes.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
-
#clear_routes ⇒ Object
Removes all routes from the router.
-
#initialize ⇒ MessageRouter
constructor
A new instance of MessageRouter.
-
#match_message(message) ⇒ Object
Returns a set of handlers that match the given message, or an empty set when none match.
-
#matches_message?(message) ⇒ Boolean
Returns true when there is at least one handler for the given message, or false otherwise.
-
#register_matchers(*matchers, handler) ⇒ Object
Registers a handler for the given matchers.
Constructor Details
#initialize ⇒ MessageRouter
Returns a new instance of MessageRouter.
12 13 14 |
# File 'lib/sequent/core/helpers/message_router.rb', line 12 def initialize clear_routes end |
Instance Attribute Details
#instanceof_routes ⇒ Object (readonly)
Returns the value of attribute instanceof_routes.
10 11 12 |
# File 'lib/sequent/core/helpers/message_router.rb', line 10 def instanceof_routes @instanceof_routes end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
10 11 12 |
# File 'lib/sequent/core/helpers/message_router.rb', line 10 def routes @routes end |
Instance Method Details
#clear_routes ⇒ Object
Removes all routes from the router.
54 55 56 57 |
# 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) ⇒ Object
Returns a set of handlers that match the given message, or an empty set when none match.
35 36 37 38 39 40 41 42 |
# File 'lib/sequent/core/helpers/message_router.rb', line 35 def () result = Set.new result.merge(@instanceof_routes[.class]) @routes.each do |matcher, handlers| result.merge(handlers) if matcher.() end result end |
#matches_message?(message) ⇒ Boolean
Returns true when there is at least one handler for the given message, or false otherwise.
47 48 49 |
# File 'lib/sequent/core/helpers/message_router.rb', line 47 def () ().any? end |
#register_matchers(*matchers, handler) ⇒ Object
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.
22 23 24 25 26 27 28 29 30 |
# 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 |