Class: Medi8::NotificationDispatcher

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

Overview

Medi8 Notification dispatcher

Instance Method Summary collapse

Constructor Details

#initialize(registry) ⇒ NotificationDispatcher

Returns a new instance of NotificationDispatcher.



20
21
22
# File 'lib/medi8/notifications.rb', line 20

def initialize(registry)
  @registry = registry
end

Instance Method Details

#publish(event) ⇒ Object

Publishes an event to all registered handlers for that event class.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/medi8/notifications.rb', line 25

def publish(event) # rubocop:disable Metrics/MethodLength
  handlers = @registry.find_notification_handlers_for(event.class)
  handlers.each do |handler_def|
    handler_class, async = handler_def

    if async
      Medi8::Jobs::NotificationJob.perform_later(
        handler_class.name,
        event.instance_variables.to_h { |var| [var.to_s.delete("@"), event.instance_variable_get(var)] },
        event.class.name
      )
    else
      handler_class.new.call(event)
    end
  end
end