Class: Cqrs::CommandBus
- Inherits:
-
Object
- Object
- Cqrs::CommandBus
- Defined in:
- lib/cqrs/command_bus.rb
Constant Summary collapse
- UnregisteredHandler =
Class.new(StandardError)
- MultipleHandlers =
Class.new(StandardError)
Instance Method Summary collapse
- #call(command) ⇒ Object
-
#initialize ⇒ CommandBus
constructor
A new instance of CommandBus.
- #register(klass, handler) ⇒ Object
Constructor Details
#initialize ⇒ CommandBus
Returns a new instance of CommandBus.
8 9 10 11 |
# File 'lib/cqrs/command_bus.rb', line 8 def initialize @handlers = ThreadSafe::Cache.new end |
Instance Method Details
#call(command) ⇒ Object
18 19 20 21 22 |
# File 'lib/cqrs/command_bus.rb', line 18 def call(command) handlers .fetch(command.class) { raise UnregisteredHandler.new("Missing handler for #{command.class}") } .(command) end |
#register(klass, handler) ⇒ Object
13 14 15 16 |
# File 'lib/cqrs/command_bus.rb', line 13 def register(klass, handler) raise MultipleHandlers.new("Multiple handlers not allowed for #{klass}") if handlers[klass] handlers[klass] = handler end |