Sha256: adc5f14f1d10fdf5f276b606b26bb07b25f3fec385cdfb1f84f98b0806fb917e

Contents?: true

Size: 1.92 KB

Versions: 6

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module SidekiqUniqueJobs
  #
  # Class NotificationCollection provides a collection with known notifications
  #
  # @author Mikael Henriksson <mikael@mhenrixon.com>
  #
  class Reflections
    #
    # @return [Array<Symbol>] list of notifications
    REFLECTIONS = [
      :after_unlock_callback_failed,
      :debug,
      :duplicate,
      :error,
      :execution_failed,
      :lock_failed,
      :locked,
      :reschedule_failed,
      :rescheduled,
      :timeout,
      :unknown_sidekiq_worker,
      :unlock_failed,
      :unlocked,
    ].freeze

    #
    # @return [Hash<Symbol, Array<Symbol, String>>] a hash with deprecated notifications
    DEPRECATIONS = {}.freeze

    REFLECTIONS.each do |reflection|
      class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
        def #{reflection}(*args, &block)                            # def unlock_failed(*args, &block)
          raise NoBlockGiven, "block required" unless block_given?  #   raise NoBlockGiven, "block required" unless block_given?
          @reflections[:#{reflection}] = block                      #   @notifications[:unlock_failed] = block
        end                                                         # end
      RUBY
    end

    def initialize
      @reflections = {}
    end

    def dispatch(reflection, *args)
      if (block = @reflections[reflection])
        block.call(*args)

        if DEPRECATIONS.key?(reflection)
          replacement, removal_version = DEPRECATIONS[reflection]
          SidekiqUniqueJobs::Deprecation.warn(
            "#{reflection} is deprecated and will be removed in version #{removal_version}. Use #{replacement} instead.",
          )
        end
      elsif misconfigured?(reflection)
        raise NoSuchNotificationError, reflection
      end
    end

    def configured?(reflection)
      REFLECTIONS.include?(reflection)
    end

    def misconfigured?(reflection)
      !configured?(reflection)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-7.1.6 lib/sidekiq_unique_jobs/reflections.rb
sidekiq-unique-jobs-7.1.5 lib/sidekiq_unique_jobs/reflections.rb
sidekiq-unique-jobs-7.1.3 lib/sidekiq_unique_jobs/reflections.rb
sidekiq-unique-jobs-7.1.2 lib/sidekiq_unique_jobs/reflections.rb
sidekiq-unique-jobs-7.1.1 lib/sidekiq_unique_jobs/reflections.rb
sidekiq-unique-jobs-7.1.0 lib/sidekiq_unique_jobs/reflections.rb