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:


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

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

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries


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

def entries
  @entries
end

Instance Method Details

#add(klass, *args) ⇒ Object


75
76
77
# File 'lib/sidekiq/middleware/chain.rb', line 75

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

#clearObject


101
102
103
# File 'lib/sidekiq/middleware/chain.rb', line 101

def clear
  entries.clear
end

#exists?(klass) ⇒ Boolean

Returns:

  • (Boolean)

93
94
95
# File 'lib/sidekiq/middleware/chain.rb', line 93

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

#insert_after(oldklass, newklass, *args) ⇒ Object


86
87
88
89
90
91
# File 'lib/sidekiq/middleware/chain.rb', line 86

def insert_after(oldklass, newklass, *args)
  i = entries.index { |entry| entry.klass == newklass }
  new_entry = i.nil? ? Entry.new(newklass, *args) : entries.delete_at(i)
  i = entries.find_index { |entry| entry.klass == oldklass } || entries.count - 1
  entries.insert(i+1, new_entry)
end

#insert_before(oldklass, newklass, *args) ⇒ Object


79
80
81
82
83
84
# File 'lib/sidekiq/middleware/chain.rb', line 79

def insert_before(oldklass, newklass, *args)
  i = entries.index { |entry| entry.klass == newklass }
  new_entry = i.nil? ? Entry.new(newklass, *args) : entries.delete_at(i)
  i = entries.find_index { |entry| entry.klass == oldklass } || 0
  entries.insert(i, new_entry)
end

#invoke(*args, &final_action) ⇒ Object


105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sidekiq/middleware/chain.rb', line 105

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


71
72
73
# File 'lib/sidekiq/middleware/chain.rb', line 71

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

#retrieveObject


97
98
99
# File 'lib/sidekiq/middleware/chain.rb', line 97

def retrieve
  entries.map(&:make_new)
end