Class: Toys::Middleware::Stack
- Inherits:
-
Object
- Object
- Toys::Middleware::Stack
- Defined in:
- lib/toys/middleware.rb
Overview
A stack of middleware specs, which can be applied in order to a tool.
A middleware stack is separated into three groups:
- #pre_specs, which are applied first.
- #default_specs, which are applied next. The default specs are set when the stack is created and are generally not modified.
- #post_specs, which are applied third.
When adding middleware to a stack, you should normally add them to the pre or post specs. By default, #add appends to the pre specs, inserting new middleware just before the defaults.
Use stack to create a middleware stack.
Instance Attribute Summary collapse
-
#default_specs ⇒ Array<Toys::Middleware:Spec>
readonly
The default set of middleware specs.
-
#post_specs ⇒ Array<Toys::Middleware:Spec>
readonly
The middleware specs that follow the default set.
-
#pre_specs ⇒ Array<Toys::Middleware:Spec>
readonly
The middleware specs that precede the default set.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Equality check.
-
#add(middleware, *args, **kwargs, &block) ⇒ Object
Add a middleware spec to the stack, in the default location, which is at the end of pre_specs).
-
#build(middleware_lookup) ⇒ Array<Toys::Middleware>
Build the middleware in this stack.
-
#dup ⇒ Toys::Middleware::Stack
Duplicate this stack.
-
#hash ⇒ Integer
Return the hash code.
Instance Attribute Details
#default_specs ⇒ Array<Toys::Middleware:Spec> (readonly)
The default set of middleware specs.
312 313 314 |
# File 'lib/toys/middleware.rb', line 312 def default_specs @default_specs end |
#post_specs ⇒ Array<Toys::Middleware:Spec> (readonly)
The middleware specs that follow the default set.
318 319 320 |
# File 'lib/toys/middleware.rb', line 318 def post_specs @post_specs end |
#pre_specs ⇒ Array<Toys::Middleware:Spec> (readonly)
The middleware specs that precede the default set.
306 307 308 |
# File 'lib/toys/middleware.rb', line 306 def pre_specs @pre_specs end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Equality check
359 360 361 362 363 364 |
# File 'lib/toys/middleware.rb', line 359 def ==(other) other.is_a?(Stack) && pre_specs.eql?(other.pre_specs) && default_specs.eql?(other.default_specs) && post_specs.eql?(other.post_specs) end |
#add(name, *args, **kwargs, &block) ⇒ Object #add(array) ⇒ Object #add(middleware_object) ⇒ Object
Add a middleware spec to the stack, in the default location, which is at the end of pre_specs). See Toys::Middleware.spec for a description of the arguments you can pass.
329 330 331 |
# File 'lib/toys/middleware.rb', line 329 def add(middleware, *args, **kwargs, &block) pre_specs.push(Middleware.spec(middleware, *args, **kwargs, &block)) end |
#build(middleware_lookup) ⇒ Array<Toys::Middleware>
Build the middleware in this stack.
349 350 351 |
# File 'lib/toys/middleware.rb', line 349 def build(middleware_lookup) (@pre_specs + @default_specs + @post_specs).map { |spec| spec.build(middleware_lookup) } end |
#dup ⇒ Toys::Middleware::Stack
Duplicate this stack.
338 339 340 341 342 |
# File 'lib/toys/middleware.rb', line 338 def dup Stack.new(pre_specs: pre_specs.dup, post_specs: post_specs.dup, default_specs: default_specs.dup) end |
#hash ⇒ Integer
Return the hash code
372 373 374 |
# File 'lib/toys/middleware.rb', line 372 def hash [@pre_specs, @default_specs, @post_specs].hash end |