Class: Booth::Coercers::Request

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/booth/coercers/request.rb

Class Method Summary collapse

Class Method Details

.call(request, initializer) ⇒ Object

If ‘DRY::Initializer` is used, then `Booth::Request` can be used as a [Coercer](dry-rb.org/gems/dry-initializer/3.0/type-constraints/#back-references).

Example:

“‘ruby class Thing

extend DRY::Initializer

option :request, ::Booth::Coercers::Request  # This uses Booth::Request.call as coercer

end

# This runs Booth::Request.call(rack_request, self). thing = Thing.new(request: rack_request) # (So in .call we have both, rack_request and the new Thing instance available). “‘

Parameters:

  • ‘request`: See initializer.

  • ‘initializer` - Some instance that called the coercer.

Returns:

  • A new Booth::Request instance.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/booth/coercers/request.rb', line 34

def self.call(request, initializer)
  # `initializer` is some instance that is trying to coerce one of its params into a `Booth::Request`.
  # By convention, that's where we assume the scope to be specified. So we take it from there.
  #
  if request.is_a?(::Booth::Request)
    return request if request.scope == initializer.scope

    # return ::Booth::Request.new(scope: initializer.scope, request: request.send(:request))

    raise "Request has #{request.scope} but #{initializer} has #{initializer.scope}"
    # request = request.send(:request)
  end

  ::Booth::Request.new(request:, scope: initializer.scope)
end