Sha256: 27dec2b9ab2dd167b270eaa411ece12eabfa384c7105fabe9171cc6f7b4cc79d

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module DispatchRider
  module Callbacks
    # Storage for callbacks.
    class Storage
      def initialize
        @callbacks = Hash.new { |storage, key| storage[key] = [] }
      end

      # @param [Symbol] event name of the event
      # @param [#call] block_param block passed as a parameter
      # @param [Proc] &block
      def before(event, block_param = nil, &block)
        around(event) do |job, *args|
          (block_param || block).call(*args)
          job.call
        end
      end

      # @param [Symbol] event name of the event
      # @param [#call] block_param block passed as a parameter
      # @param [Proc] &block
      def after(event, block_param = nil, &block)
        around(event) do |job, *args|

          job.call
        ensure
          (block_param || block).call(*args)

        end
      end

      # @param [Symbol] event name of the event
      # @param [#call] block_param block passed as a parameter
      # @param [Proc] &block
      def around(event, block_param = nil, &block)
        @callbacks[event] << (block_param || block)
      end

      # @param [Symbol] event name of the event
      def for(event)
        @callbacks[event]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dispatch-rider-2.2.0 lib/dispatch-rider/callbacks/storage.rb