Class: Sidekiq::Middleware::Chain
- Inherits:
-
Object
- Object
- Sidekiq::Middleware::Chain
- Includes:
- Enumerable
- Defined in:
- lib/sidekiq/middleware/chain.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Instance Method Summary collapse
- #add(klass, *args) ⇒ Object
- #clear ⇒ Object
- #each(&block) ⇒ Object
- #exists?(klass) ⇒ Boolean
-
#initialize {|_self| ... } ⇒ Chain
constructor
A new instance of Chain.
- #initialize_copy(copy) ⇒ Object
- #insert_after(oldklass, newklass, *args) ⇒ Object
- #insert_before(oldklass, newklass, *args) ⇒ Object
- #invoke(*args) ⇒ Object
- #prepend(klass, *args) ⇒ Object
- #remove(klass) ⇒ Object
- #retrieve ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Chain
Returns a new instance of Chain.
78 79 80 81 |
# File 'lib/sidekiq/middleware/chain.rb', line 78 def initialize @entries = [] yield self if block_given? end |
Instance Attribute Details
#entries ⇒ Object (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 |
#clear ⇒ Object
119 120 121 |
# File 'lib/sidekiq/middleware/chain.rb', line 119 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
111 112 113 |
# File 'lib/sidekiq/middleware/chain.rb', line 111 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
104 105 106 107 108 109 |
# File 'lib/sidekiq/middleware/chain.rb', line 104 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
97 98 99 100 101 102 |
# File 'lib/sidekiq/middleware/chain.rb', line 97 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
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/sidekiq/middleware/chain.rb', line 123 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
92 93 94 95 |
# File 'lib/sidekiq/middleware/chain.rb', line 92 def prepend(klass, *args) remove(klass) if exists?(klass) entries.insert(0, Entry.new(klass, *args)) 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 |
#retrieve ⇒ Object
115 116 117 |
# File 'lib/sidekiq/middleware/chain.rb', line 115 def retrieve map(&:make_new) end |