Class: Acfs::Service::Middleware::Stack
- Inherits:
-
Object
- Object
- Acfs::Service::Middleware::Stack
- Includes:
- Enumerable
- Defined in:
- lib/acfs/service/middleware/stack.rb
Constant Summary collapse
- MUTEX =
Mutex.new
- IDENTITY =
->(i) { i }
Instance Attribute Summary collapse
-
#middlewares ⇒ Object
readonly
Returns the value of attribute middlewares.
Instance Method Summary collapse
- #build(app = IDENTITY) ⇒ Object
- #build! ⇒ Object
- #call(request) ⇒ Object
- #clear ⇒ Object
- #each ⇒ Object
-
#initialize ⇒ Stack
constructor
A new instance of Stack.
- #insert(index, klass, *args, &block) ⇒ Object
Constructor Details
#initialize ⇒ Stack
Returns a new instance of Stack.
14 15 16 |
# File 'lib/acfs/service/middleware/stack.rb', line 14 def initialize @middlewares = [] end |
Instance Attribute Details
#middlewares ⇒ Object (readonly)
Returns the value of attribute middlewares.
12 13 14 |
# File 'lib/acfs/service/middleware/stack.rb', line 12 def middlewares @middlewares end |
Instance Method Details
#build(app = IDENTITY) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/acfs/service/middleware/stack.rb', line 34 def build(app = IDENTITY) middlewares.reverse.inject(app) do |next_middleware, current_middleware| klass, args, block = current_middleware args ||= [] if klass.is_a?(Class) klass.new(next_middleware, *args, &block) elsif klass.respond_to?(:call) lambda do |env| next_middleware.call(klass.call(env, *args)) end else raise "Invalid middleware, doesn't respond to `call`: #{klass.inspect}" end end end |
#build! ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/acfs/service/middleware/stack.rb', line 24 def build! return if @app MUTEX.synchronize do return if @app @app = build end end |
#call(request) ⇒ Object
18 19 20 21 22 |
# File 'lib/acfs/service/middleware/stack.rb', line 18 def call(request) build! unless @app @app.call request end |
#clear ⇒ Object
59 60 61 |
# File 'lib/acfs/service/middleware/stack.rb', line 59 def clear middlewares.clear end |
#each ⇒ Object
55 56 57 |
# File 'lib/acfs/service/middleware/stack.rb', line 55 def each middlewares.each {|x| yield x.first } end |
#insert(index, klass, *args, &block) ⇒ Object
51 52 53 |
# File 'lib/acfs/service/middleware/stack.rb', line 51 def insert(index, klass, *args, &block) middlewares.insert(index, [klass, args, block]) end |