Class: GlassOctopus::Builder
- Inherits:
-
Object
- Object
- GlassOctopus::Builder
- Defined in:
- lib/glass_octopus/builder.rb
Overview
GlassOctopus::Builder is a small DLS to build processing pipelines. It is very similar to Rack::Builder.
Middleware can be a class with a similar signature to Rack middleware. The constructor needs to take an app
object which is basically the next middleware in the stack and the instance of the class has to respond to #call(ctx).
Lambdas/procs can also be used as middleware. In this case the middleware does not have control over the execution of the next middleware: the next one is always called.
Instance Method Summary collapse
-
#call(context) ⇒ void
Generate the middleware stack and call it with the passed in context.
-
#initialize(&block) ⇒ Builder
constructor
A new instance of Builder.
-
#run(app) ⇒ Builder
Sets the final step in the middleware pipeline, essentially the application itself.
-
#to_app ⇒ #call
Generate a new middleware stack from the registered entries.
-
#use(klass, *args, &block) ⇒ Builder
Append a middleware to the stack.
Constructor Details
#initialize(&block) ⇒ Builder
Returns a new instance of Builder.
51 52 53 54 55 56 |
# File 'lib/glass_octopus/builder.rb', line 51 def initialize(&block) @entries = [] @app = ->(ctx) {} instance_eval(&block) if block_given? end |
Instance Method Details
#call(context) ⇒ void
This method instantiates a new middleware stack on every call.
This method returns an undefined value.
Generate the middleware stack and call it with the passed in context.
94 95 96 |
# File 'lib/glass_octopus/builder.rb', line 94 def call(context) to_app.call(context) end |
#run(app) ⇒ Builder
Sets the final step in the middleware pipeline, essentially the application itself. Takes a parameter that responds to #call(ctx).
74 75 76 77 |
# File 'lib/glass_octopus/builder.rb', line 74 def run(app) @app = app self end |
#to_app ⇒ #call
Generate a new middleware stack from the registered entries.
83 84 85 86 87 |
# File 'lib/glass_octopus/builder.rb', line 83 def to_app @entries.reverse_each.reduce(@app) do |app, current_entry| current_entry.build(app) end end |