Created
August 7, 2018 16:04
-
-
Save we4tech/fbea5f0a9280087779b51a0527e7f850 to your computer and use it in GitHub Desktop.
How to chain middleware call in Ruby?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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