Class: Claps::Bus

Inherits:
Object
  • Object
show all
Defined in:
lib/claps/bus.rb,
lib/claps/bus/version.rb

Overview

Bus to handle query or command events

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initializeBus

Returns a new instance of Bus.



13
14
15
# File 'lib/claps/bus.rb', line 13

def initialize
  @handlers = Concurrent::Map.new
end

Instance Method Details

#call(command, *args, **kwargs) ⇒ Object



23
24
25
26
27
# File 'lib/claps/bus.rb', line 23

def call(command, *args, **kwargs)
  handlers.fetch(command) do
    raise UnregisteredHandlerError, "Missing handler for #{command}"
  end.call(*args, **kwargs)
end

#register(command:, handler:) ⇒ Object



17
18
19
20
21
# File 'lib/claps/bus.rb', line 17

def register(command:, handler:)
  raise MultipleHandlerError, "#{command} has multiple handers registered" if handlers[command]

  handlers[command] = handler
end