Class: Medi8::Mediator

Inherits:
Object
  • Object
show all
Defined in:
lib/medi8/mediator.rb

Overview

Mediator is the main entry point for handling requests and publishing events.

Instance Method Summary collapse

Constructor Details

#initialize(registry) ⇒ Mediator

Returns a new instance of Mediator.



6
7
8
# File 'lib/medi8/mediator.rb', line 6

def initialize(registry)
  @registry = registry
end

Instance Method Details

#publish(event) ⇒ Object

Publishes an event to all registered subscribers.



25
26
27
# File 'lib/medi8/mediator.rb', line 25

def publish(event)
  NotificationDispatcher.new(@registry).publish(event)
end

#send(request) ⇒ Object

Sends a request to the appropriate handler.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/medi8/mediator.rb', line 11

def send(request)
  handler_class = @registry.find_handler_for(request.class)
  raise "No handler registered for #{request.class}" unless handler_class

  final = -> { handler_class.new.call(request) }

  if defined?(Medi8.middleware_stack)
    Medi8.middleware_stack.call(request, &final)
  else
    final.call
  end
end