Class: Hermes::EventHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/hermes/event_handler.rb

Defined Under Namespace

Classes: Registration

Instance Method Summary collapse

Constructor Details

#initialize(container: Dry::Container.new, consumer_builder: Hermes::DependenciesContainer["consumer_builder"]) ⇒ EventHandler

Returns a new instance of EventHandler.



8
9
10
11
# File 'lib/hermes/event_handler.rb', line 8

def initialize(container: Dry::Container.new, consumer_builder: Hermes::DependenciesContainer["consumer_builder"])
  @container = container
  @consumer_builder = consumer_builder
end

Instance Method Details

#handle(event_class, with:, async: true, rpc: false, consumer_config: -> {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hermes/event_handler.rb', line 17

def handle(event_class, with:, async: true, rpc: false, consumer_config: -> {})
  handler = with
  options = {
    async: async,
    rpc: rpc,
    consumer_config: consumer_config
  }
  consumer = build_consumer_for_event(event_class, consumer_config)

  Registration.new(handler, consumer, options).tap do |registration|
    container.register(event_class, registration)
  end
end

#handle_events(&block) ⇒ Object



13
14
15
# File 'lib/hermes/event_handler.rb', line 13

def handle_events(&block)
  instance_exec(&block)
end

#registration_for(event_class) ⇒ Object



31
32
33
# File 'lib/hermes/event_handler.rb', line 31

def registration_for(event_class)
  container.resolve(event_class)
end