Sha256: 9adb1bc4446a7fc831d152e84c60b82a9dc850e1356e5799d880d68ca6bfadb0

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module Qyu
  module Workers
    module Concerns
      # 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
      #
      module Callback
        # 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
        #
        # @param [Symbol] just :execute for now
        # @param [Symbol] callback type
        # @param [block] block to execute as callback
        def callback(method, type, &block)
          @_callbacks ||= {}
          @_callbacks[method] ||= {}
          @_callbacks[method][type] = block
        end

        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

        private

        def find_callback(method, type)
          @_callbacks.dig(method, type) if @_callbacks.is_a?(Hash)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
qyu-1.1.0 lib/qyu/workers/concerns/callback.rb
qyu-1.0.2 lib/qyu/workers/concerns/callback.rb