Class: Sidekiq::Middleware::Chain

Inherits:
Object
  • Object
show all
Includes:
Enumerable
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:


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

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

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries


69
70
71
# File 'lib/sidekiq/middleware/chain.rb', line 69

def entries
  @entries
end

Instance Method Details

#add(klass, *args) ⇒ Object


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

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

#clearObject


120
121
122
# File 'lib/sidekiq/middleware/chain.rb', line 120

def clear
  entries.clear
end

#each(&block) ⇒ Object


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

def each(&block)
  entries.each(&block)
end

#exists?(klass) ⇒ Boolean

Returns:

  • (Boolean)

112
113
114
# File 'lib/sidekiq/middleware/chain.rb', line 112

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

#initialize_copy(copy) ⇒ Object


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

def initialize_copy(copy)
  copy.instance_variable_set(:@entries, entries.dup)
end

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


105
106
107
108
109
110
# File 'lib/sidekiq/middleware/chain.rb', line 105

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.index { |entry| entry.klass == oldklass } || entries.count - 1
  entries.insert(i+1, new_entry)
end

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


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

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.index { |entry| entry.klass == oldklass } || 0
  entries.insert(i, new_entry)
end

#invoke(*args) ⇒ Object


124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sidekiq/middleware/chain.rb', line 124

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

#prepend(klass, *args) ⇒ Object


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

def prepend(klass, *args)
  remove(klass) if exists?(klass)
  entries.insert(0, Entry.new(klass, *args))
end

#remove(klass) ⇒ Object


84
85
86
# File 'lib/sidekiq/middleware/chain.rb', line 84

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

#retrieveObject


116
117
118
# File 'lib/sidekiq/middleware/chain.rb', line 116

def retrieve
  map(&:make_new)
end