Module: Qyu::Workers::Concerns::Callback

Included in:
Base
Defined in:
lib/qyu/workers/concerns/callback.rb

Overview

Qyu::Workers::Concerns::Callback

Meant to add callbacks to Qyu::Worker

Usage:

Qyu::Worker.new do

callback :execute, :after do
  # Do something after execution
end

end

Instance Method Summary collapse

Instance Method Details

#callback(method, type, &block) ⇒ Object

Adds a callback option to worker

callback :execute, :after do
  # Do something after execution
end

callback :execute, :before do
  # Do something before execution
end

callback :execute, :around do
  # Do something around execution
end

36
37
38
39
40
# File 'lib/qyu/workers/concerns/callback.rb', line 36

def callback(method, type, &block)
  @_callbacks ||= {}
  @_callbacks[method] ||= {}
  @_callbacks[method][type] = block
end

#run_callbacks(method, &block) ⇒ Object


42
43
44
45
46
# File 'lib/qyu/workers/concerns/callback.rb', line 42

def run_callbacks(method, &block)
  find_callback(method, :before)&.call
  find_callback(method, :around) ? find_callback(method, :around).call(block) : yield
  find_callback(method, :after)&.call
end