Class: Sidekiq::Middleware::Chain

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

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 = nil
  yield self if block_given?
end

Instance Method Details

#add(klass, *args) ⇒ Object



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

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

#clearObject



128
129
130
# File 'lib/sidekiq/middleware/chain.rb', line 128

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @entries.nil? || @entries.empty?
end

#entriesObject



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

def entries
  @entries ||= []
end

#exists?(klass) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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



102
103
104
105
106
107
# File 'lib/sidekiq/middleware/chain.rb', line 102

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



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sidekiq/middleware/chain.rb', line 132

def invoke(*args)
  return yield if empty?

  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



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

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

#remove(klass) ⇒ Object



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

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

#retrieveObject



124
125
126
# File 'lib/sidekiq/middleware/chain.rb', line 124

def retrieve
  map(&:make_new)
end