Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to chain middleware call in Ruby?
# Create a middleware class
class Middleware
def call(*args)
puts "Args: #{args}"
yield
end
end
def call_in_chain(chain)
traverse_chain = lambda do
if chain.empty?
yield
else
chain.shift.call(chain.size, &traverse_chain)
end
end
traverse_chain.call
end
# Example call
call_in_chain(Array.new(5) { Middleware.new }) { puts 'Finally the unit of work' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.