Class: Interceptors::Pipeline
- Inherits:
-
Object
- Object
- Interceptors::Pipeline
- Defined in:
- lib/interceptors/pipeline.rb
Instance Method Summary collapse
- #call(ctx, &final_handler) ⇒ Object
-
#initialize(interceptors) ⇒ Pipeline
constructor
A new instance of Pipeline.
Constructor Details
#initialize(interceptors) ⇒ Pipeline
Returns a new instance of Pipeline.
5 6 7 |
# File 'lib/interceptors/pipeline.rb', line 5 def initialize(interceptors) @interceptors = Array(interceptors) end |
Instance Method Details
#call(ctx, &final_handler) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/interceptors/pipeline.rb', line 9 def call(ctx, &final_handler) raise ArgumentError, "a final handler block is required" unless final_handler chain = @interceptors.reverse.inject(final_handler) do |acc, interceptor| proc do |inner_ctx| interceptor.before(inner_ctx) result = interceptor.around(inner_ctx) { |next_ctx| acc.call(next_ctx) } interceptor.after(inner_ctx, result) || result end end chain.call(ctx) end |