Skip to content

Instantly share code, notes, and snippets.

@airled
Last active June 16, 2020 06:56
Show Gist options
  • Save airled/38c82b4a4b4f13ea1cd15531dfb1d302 to your computer and use it in GitHub Desktop.
Save airled/38c82b4a4b4f13ea1cd15531dfb1d302 to your computer and use it in GitHub Desktop.
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