Class: RabbitmqClient::Lifecycle

Inherits:
Object
  • Object
show all
Defined in:
lib/rabbitmq_client/lifecycle.rb

Overview

Lifecycle defines the rabbitmq_client lifecycle events, callbacks and manage the execution of these callbacks

Constant Summary collapse

EVENTS =
{
  publish: %i[message options]
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLifecycle

Returns a new instance of Lifecycle.



15
16
17
18
19
# File 'lib/rabbitmq_client/lifecycle.rb', line 15

def initialize
  @callbacks = EVENTS.keys.each_with_object({}) do |key, hash|
    hash[key] = Callback.new
  end
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



13
14
15
# File 'lib/rabbitmq_client/lifecycle.rb', line 13

def callbacks
  @callbacks
end

Instance Method Details

#after(event, &block) ⇒ Object



25
26
27
# File 'lib/rabbitmq_client/lifecycle.rb', line 25

def after(event, &block)
  add(:after, event, &block)
end

#before(event, &block) ⇒ Object



21
22
23
# File 'lib/rabbitmq_client/lifecycle.rb', line 21

def before(event, &block)
  add(:before, event, &block)
end

#run_callbacks(event, *args, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rabbitmq_client/lifecycle.rb', line 29

def run_callbacks(event, *args, &block)
  missing_callback(event) unless @callbacks.key?(event)
  event_obj = EVENTS[event]
  event_size = event_obj.size

  unless event_size == args.size
    raise ArgumentError, "Callback #{event} expects\
     #{event_size} parameter(s): #{event_obj.join(', ')}"
  end

  @callbacks[event].execute(*args, &block)
end