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:



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

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

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



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

def entries
  @entries
end

Instance Method Details

#add(klass, *args) ⇒ Object



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

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

#clearObject



114
115
116
# File 'lib/sidekiq/middleware/chain.rb', line 114

def clear
  entries.clear
end

#each(&block) ⇒ Object



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

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

#exists?(klass) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#initialize_copy(copy) ⇒ Object



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

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

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



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

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



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

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, &final_action) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sidekiq/middleware/chain.rb', line 118

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



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

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

#retrieveObject



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

def retrieve
  map(&:make_new)
end