Class: Toys::Middleware::Spec

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#argsArray? (readonly)

Returns:

  • (Array)

    the positional arguments to be passed to a middleware class constructor, or the empty array if there are no positional arguments

  • (nil)

    if this spec wraps a middleware object



226
227
228
# File 'lib/toys/middleware.rb', line 226

def args
  @args
end

#blockProc? (readonly)

Returns:

  • (Proc)

    if there is a block argument to be passed to a middleware class constructor

  • (nil)

    if there is no block argument, or this spec wraps a middleware object



241
242
243
# File 'lib/toys/middleware.rb', line 241

def block
  @block
end

#kwargsHash? (readonly)

Returns:

  • (Hash)

    the keyword arguments to be passed to a middleware class constructor, or the empty hash if there are no keyword arguments

  • (nil)

    if this spec wraps a middleware object



233
234
235
# File 'lib/toys/middleware.rb', line 233

def kwargs
  @kwargs
end

#nameString, ... (readonly)

Returns:

  • (String, Symbol)

    if this spec represents a middleware name

  • (Class)

    if this spec represents a middleware class

  • (nil)

    if this spec wraps a middleware object



218
219
220
# File 'lib/toys/middleware.rb', line 218

def name
  @name
end

#objectToys::Middleware? (readonly)

Returns:

  • (Toys::Middleware)

    if this spec wraps a middleware object

  • (nil)

    if this spec represents a class to instantiate



211
212
213
# File 'lib/toys/middleware.rb', line 211

def object
  @object
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Equality check

Parameters:

  • other (Object)

Returns:

  • (Boolean)


249
250
251
252
253
254
255
256
# File 'lib/toys/middleware.rb', line 249

def ==(other)
  other.is_a?(Spec) &&
    object.eql?(other.object) &&
    name.eql?(other.name) &&
    args.eql?(other.args) &&
    kwargs.eql?(other.kwargs) &&
    block.eql?(other.block)
end

#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.

Parameters:

Returns:



196
197
198
199
200
201
202
203
204
205
# File 'lib/toys/middleware.rb', line 196

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
  Compat.instantiate(klass, @args, @kwargs, @block)
end

#hashInteger

Return the hash code

Returns:

  • (Integer)


264
265
266
# File 'lib/toys/middleware.rb', line 264

def hash
  [object, name, args, kwargs, block].hash
end