Class: Toys::Middleware::Spec
- Inherits:
-
Object
- Object
- Toys::Middleware::Spec
- Defined in:
- lib/toys/middleware.rb
Overview
A middleware specification, including the middleware class and the arguments to pass to the constructor.
Use spec to create a middleware spec.
Instance Attribute Summary collapse
- #args ⇒ Array? readonly
- #block ⇒ Proc? readonly
- #kwargs ⇒ Hash? readonly
- #name ⇒ String, ... readonly
- #object ⇒ Toys::Middleware? readonly
Instance Method Summary collapse
-
#build(lookup) ⇒ Toys::Middleware
Builds a middleware for this spec, given a ModuleLookup for middleware.
Instance Attribute Details
#args ⇒ Array? (readonly)
248 249 250 |
# File 'lib/toys/middleware.rb', line 248 def args @args end |
#block ⇒ Proc? (readonly)
263 264 265 |
# File 'lib/toys/middleware.rb', line 263 def block @block end |
#kwargs ⇒ Hash? (readonly)
255 256 257 |
# File 'lib/toys/middleware.rb', line 255 def kwargs @kwargs end |
#name ⇒ String, ... (readonly)
240 241 242 |
# File 'lib/toys/middleware.rb', line 240 def name @name end |
#object ⇒ Toys::Middleware? (readonly)
233 234 235 |
# File 'lib/toys/middleware.rb', line 233 def object @object end |
Instance Method Details
#build(lookup) ⇒ Toys::Middleware
Builds a middleware for this spec, given a ModuleLookup for middleware.
If this spec wraps an existing middleware object, returns that object. Otherwise, constructs a middleware object from the spec.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/toys/middleware.rb', line 211 def build(lookup) return @object unless @object.nil? if @name.is_a?(::String) || @name.is_a?(::Symbol) klass = lookup&.lookup(@name) raise ::NameError, "Unknown middleware name #{@name.inspect}" if klass.nil? else klass = @name end # Due to a bug in Ruby < 2.7, passing an empty **kwargs splat to # initialize will fail if there are no formal keyword args. formals = klass.instance_method(:initialize).parameters if @kwargs.empty? && formals.all? { |arg| arg.first != :key && arg.first != :keyrest } klass.new(*@args, &@block) else klass.new(*@args, **@kwargs, &@block) end end |