Class: Shoryuken::Middleware::Chain
- Inherits:
-
Object
- Object
- Shoryuken::Middleware::Chain
- Defined in:
- lib/shoryuken/middleware/chain.rb
Overview
Manages a chain of middleware classes that will be instantiated and invoked in sequence around message processing. Provides methods for adding, removing, and reordering middleware.
Instance Attribute Summary collapse
-
#entries ⇒ Array<Entry>
readonly
The ordered list of middleware entries.
Instance Method Summary collapse
-
#add(klass, *args) ⇒ Object
Adds middleware to the end of the chain.
-
#clear ⇒ Array
Removes all middleware from the chain.
-
#dup ⇒ Chain
Creates a copy of this middleware chain.
-
#exists?(klass) ⇒ Boolean
Checks if a middleware class is already in the chain.
-
#initialize {|Chain| ... } ⇒ Chain
constructor
Creates a new middleware chain.
-
#insert_after(oldklass, newklass, *args) ⇒ Object
Inserts middleware immediately after another middleware class.
-
#insert_before(oldklass, newklass, *args) ⇒ Object
Inserts middleware immediately before another middleware class.
-
#invoke(*args) { ... } ⇒ Object
Invokes the middleware chain with the given arguments.
-
#prepend(klass, *args) ⇒ Object
Adds middleware to the beginning of the chain.
-
#remove(klass) ⇒ Array<Entry>
Removes all instances of the specified middleware class from the chain.
-
#retrieve ⇒ Array
Creates instances of all middleware classes in the chain.
Constructor Details
#initialize {|Chain| ... } ⇒ Chain
Creates a new middleware chain.
102 103 104 105 |
# File 'lib/shoryuken/middleware/chain.rb', line 102 def initialize @entries = [] yield self if block_given? end |
Instance Attribute Details
#entries ⇒ Array<Entry> (readonly)
Returns The ordered list of middleware entries.
92 93 94 |
# File 'lib/shoryuken/middleware/chain.rb', line 92 def entries @entries end |
Instance Method Details
#add(klass, *args) ⇒ Object
Adds middleware to the end of the chain. Does nothing if the middleware class is already present in the chain.
131 132 133 |
# File 'lib/shoryuken/middleware/chain.rb', line 131 def add(klass, *args) entries << Entry.new(klass, *args) unless exists?(klass) end |
#clear ⇒ Array
Removes all middleware from the chain.
194 195 196 |
# File 'lib/shoryuken/middleware/chain.rb', line 194 def clear entries.clear end |
#dup ⇒ Chain
Creates a copy of this middleware chain.
110 111 112 |
# File 'lib/shoryuken/middleware/chain.rb', line 110 def dup self.class.new.tap { |new_chain| new_chain.entries.replace(entries) } end |
#exists?(klass) ⇒ Boolean
Checks if a middleware class is already in the chain.
180 181 182 |
# File 'lib/shoryuken/middleware/chain.rb', line 180 def exists?(klass) entries.any? { |entry| entry.klass == klass } end |
#insert_after(oldklass, newklass, *args) ⇒ Object
Inserts middleware immediately after another middleware class. If the new middleware already exists, it’s moved to the new position.
169 170 171 172 173 174 |
# File 'lib/shoryuken/middleware/chain.rb', line 169 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
Inserts middleware immediately before another middleware class. If the new middleware already exists, it’s moved to the new position.
154 155 156 157 158 159 |
# File 'lib/shoryuken/middleware/chain.rb', line 154 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) { ... } ⇒ Object
Invokes the middleware chain with the given arguments. Each middleware’s call method will be invoked in sequence, with control passed through yielding.
204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/shoryuken/middleware/chain.rb', line 204 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 |
#prepend(klass, *args) ⇒ Object
Adds middleware to the beginning of the chain. Does nothing if the middleware class is already present in the chain.
142 143 144 |
# File 'lib/shoryuken/middleware/chain.rb', line 142 def prepend(klass, *args) entries.insert(0, Entry.new(klass, *args)) unless exists?(klass) end |
#remove(klass) ⇒ Array<Entry>
Removes all instances of the specified middleware class from the chain.
120 121 122 |
# File 'lib/shoryuken/middleware/chain.rb', line 120 def remove(klass) entries.delete_if { |entry| entry.klass == klass } end |
#retrieve ⇒ Array
Creates instances of all middleware classes in the chain.
187 188 189 |
# File 'lib/shoryuken/middleware/chain.rb', line 187 def retrieve entries.map(&:make_new) end |