Sha256: bd922a4a1772be39fd77a0835ecc90fa3d7f29be042b8df4754d4a68f3f52caf

Contents?: true

Size: 787 Bytes

Versions: 5

Compression:

Stored size: 787 Bytes

Contents

# frozen_string_literal: true

module Delayed
  module Master
    class Callbacks
      def initialize(config)
        @config = config
      end

      def call(event, *args, &block)
        run(:"before_#{event}", *args)
        result = run(:"around_#{event}", *args, &block)
        run(:"after_#{event}", *args)
        result
      end

      def run(name, *args, &block)
        callbacks = get_callbacks(name)
        if block
          callbacks.reverse.reduce(block) { |ret, c| -> { c.call(*args, &ret) } }.call
        else
          callbacks.each { |c| c.call(*args) }
        end
      end

      private

      def get_callbacks(name)
        if @config.respond_to?(name)
          @config.send(name).to_a
        else
          []
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
delayed_job_master-3.1.2 lib/delayed/master/callbacks.rb
delayed_job_master-3.1.1 lib/delayed/master/callbacks.rb
delayed_job_master-3.1.0 lib/delayed/master/callbacks.rb
delayed_job_master-3.0.1 lib/delayed/master/callbacks.rb
delayed_job_master-3.0.0 lib/delayed/master/callbacks.rb