Class: Rhod::Middleware
- Inherits:
-
Object
- Object
- Rhod::Middleware
- Defined in:
- lib/rhod/middleware.rb
Instance Method Summary collapse
- #build_stack ⇒ Object
-
#initialize ⇒ Middleware
constructor
A new instance of Middleware.
- #on(event, env) ⇒ Object
- #use(middleware, *args, &block) ⇒ Object
Constructor Details
#initialize ⇒ Middleware
Returns a new instance of Middleware.
4 5 6 |
# File 'lib/rhod/middleware.rb', line 4 def initialize @stack = [] end |
Instance Method Details
#build_stack ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rhod/middleware.rb', line 12 def build_stack @stack = @stack.map do |current_middleware| klass, args, block = current_middleware if klass.is_a?(Class) klass.new(*args, &block) else raise Rhod::InvalidMiddleware, "Unable to call middleware #{current_middleware}" end end end |
#on(event, env) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/rhod/middleware.rb', line 24 def on(event, env) @stack.reduce(env) do |e, current_middleware| if current_middleware.respond_to?(:on) e = current_middleware.on(event, e) end e end end |
#use(middleware, *args, &block) ⇒ Object
8 9 10 |
# File 'lib/rhod/middleware.rb', line 8 def use(middleware, *args, &block) @stack << [middleware, args, block] end |