Class: Aggro::MessageRouter
- Inherits:
-
Object
- Object
- Aggro::MessageRouter
- Defined in:
- lib/aggro/message_router.rb
Overview
Public: Routes inter-node messages to attached message handlers.
Instance Method Summary collapse
- #attach_handler(message_class, callable = nil, &block) ⇒ Object
- #handles?(message_class) ⇒ Boolean
-
#initialize ⇒ MessageRouter
constructor
A new instance of MessageRouter.
- #route(message) ⇒ Object
Constructor Details
#initialize ⇒ MessageRouter
Returns a new instance of MessageRouter.
4 5 6 |
# File 'lib/aggro/message_router.rb', line 4 def initialize @handlers = {} end |
Instance Method Details
#attach_handler(message_class, callable = nil, &block) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/aggro/message_router.rb', line 8 def attach_handler(, callable = nil, &block) if callable @handlers[] = callable elsif block_given? @handlers[] = block else fail ArgumentError end end |
#handles?(message_class) ⇒ Boolean
18 19 20 |
# File 'lib/aggro/message_router.rb', line 18 def handles?() @handlers[] end |
#route(message) ⇒ Object
22 23 24 |
# File 'lib/aggro/message_router.rb', line 22 def route() @handlers[.class].call if handles?(.class) end |