Class: Sidekiq::Middleware::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/middleware/chain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Chain

Returns a new instance of Chain.

Yields:

  • (_self)

Yield Parameters:



46
47
48
49
# File 'lib/sidekiq/middleware/chain.rb', line 46

def initialize
  @entries = []
  yield self if block_given?
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



44
45
46
# File 'lib/sidekiq/middleware/chain.rb', line 44

def entries
  @entries
end

Instance Method Details

#add(klass, *args) ⇒ Object



55
56
57
# File 'lib/sidekiq/middleware/chain.rb', line 55

def add(klass, *args)
  entries << Entry.new(klass, *args) unless exists?(klass)
end

#clearObject



67
68
69
# File 'lib/sidekiq/middleware/chain.rb', line 67

def clear
  entries.clear
end

#exists?(klass) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/sidekiq/middleware/chain.rb', line 59

def exists?(klass)
  entries.any? { |entry| entry.klass == klass }
end

#invoke(*args, &final_action) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sidekiq/middleware/chain.rb', line 71

def invoke(*args, &final_action)
  chain = retrieve.dup
  traverse_chain = lambda do
    if chain.empty?
      final_action.call
    else
      chain.shift.call(*args, &traverse_chain)
    end
  end
  traverse_chain.call
end

#remove(klass) ⇒ Object



51
52
53
# File 'lib/sidekiq/middleware/chain.rb', line 51

def remove(klass)
  entries.delete_if { |entry| entry.klass == klass }
end

#retrieveObject



63
64
65
# File 'lib/sidekiq/middleware/chain.rb', line 63

def retrieve
  entries.map(&:make_new)
end