Last active
June 16, 2020 06:56
-
-
Save airled/38c82b4a4b4f13ea1cd15531dfb1d302 to your computer and use it in GitHub Desktop.
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
pipeline = [] | |
define_method :use, -> middleware do | |
pipeline.unshift(middleware) | |
end | |
class One | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
env[:one] = '1' | |
@app.call(env) | |
end | |
end | |
class Two | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
env[:two] = '2' | |
@app.call(env) | |
end | |
end | |
class Three | |
def initialize(_app) | |
end | |
def call(env) | |
env[:three] = '3' | |
end | |
end | |
use One | |
use Two | |
use Three | |
app = nil | |
pipeline.each do |middleware| | |
app = middleware.new(app) | |
end | |
env = {start: :ok} | |
app.call(env) | |
p env |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment