Class: Faulty::Events::CallbackListener

Inherits:
Object
  • Object
show all
Defined in:
lib/faulty/events/callback_listener.rb

Overview

A simple listener implementation that uses callback blocks as handlers

Each event in EVENTS has a method on this class that can be used to register a callback for that event.

Examples:

listener = CallbackListener.new
listener.circuit_opened do |payload|
  logger.error(
    "Circuit #{payload[:circuit].name} opened: #{payload[:error].message}"
  )
end

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ CallbackListener

Returns a new instance of CallbackListener.

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
# File 'lib/faulty/events/callback_listener.rb', line 18

def initialize
  @handlers = {}
  yield self if block_given?
end

Instance Method Details

#handle(event, payload) ⇒ void

This method returns an undefined value.

Parameters:

  • event (Symbol)

    The event name. Will be a member of EVENTS.

  • payload (Hash)

    A hash with keys based on the event type



25
26
27
28
29
30
31
32
# File 'lib/faulty/events/callback_listener.rb', line 25

def handle(event, payload)
  return unless EVENT_SET.include?(event)
  return unless @handlers.key?(event)

  @handlers[event].each do |handler|
    handler.call(payload)
  end
end