Class: Insika::CommandBus

Inherits:
Object
  • Object
show all
Defined in:
lib/insika/command_bus.rb

Overview

Routes Commands to handlers registered by the composition root. The bus does NOT distinguish control Commands (synchronous response) from turn Commands (immediate {task_id:}) — that is the handler's job; the bus only routes.

No lock/mutex: one reactor, cooperative fibers; dispatch does no IO of its own.

Instance Method Summary collapse

Constructor Details

#initializeCommandBus

Handlers receive their dependencies in their own constructor and emit on their own — the bus only routes.



14
15
16
# File 'lib/insika/command_bus.rb', line 14

def initialize
  @handlers = {}
end

Instance Method Details

#dispatch(command) ⇒ Object

-> the handler's result. Unregistered type -> synchronous ValidationError, no Task created (never KeyError/NoMethodError).



32
33
34
35
36
37
# File 'lib/insika/command_bus.rb', line 32

def dispatch(command)
  handler = @handlers[command.type]
  raise Insika::ValidationError, "unknown command: #{command.type}" if handler.nil?

  handler.call(command)
end

#register(type, handler) ⇒ Object

handler: any object responding to #call(command). Re-registering the same type overwrites (last wins — the composition root is the only caller).



21
22
23
# File 'lib/insika/command_bus.rb', line 21

def register(type, handler)
  @handlers[type.to_sym] = handler
end

#registered?(type) ⇒ Boolean

Introspection for the composition roots / graph specs: which command types the bus can route. Read-only; does not expose the handlers.

Returns:

  • (Boolean)


27
# File 'lib/insika/command_bus.rb', line 27

def registered?(type) = @handlers.key?(type.to_sym)

#typesObject



28
# File 'lib/insika/command_bus.rb', line 28

def types = @handlers.keys