Class: Otto::RouteHandlers::HandlerFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/otto/route_handlers/factory.rb

Overview

Factory for creating appropriate handlers based on route definitions

Class Method Summary collapse

Class Method Details

.apply_handler_wrappers(handler, route_definition, otto_instance) ⇒ Object

Compose registered wrappers outermost-first. Built innermost-out so reverse_each yields a final order of: w1(w2(…(RouteAuthWrapper(base)))).



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/otto/route_handlers/factory.rb', line 45

def self.apply_handler_wrappers(handler, route_definition, otto_instance)
  wrappers = otto_instance&.handler_wrappers
  return handler if wrappers.nil? || wrappers.empty?

  wrappers.reverse_each do |factory|
    wrapped = factory.call(route_definition, handler)
    unless wrapped.respond_to?(:call)
      raise TypeError,
            "handler wrapper must return an object responding to :call, got #{wrapped.class}"
    end
    handler = wrapped
  end
  handler
end

.create_handler(route_definition, otto_instance = nil) ⇒ BaseHandler

Create a handler for the given route definition

Parameters:

  • route_definition (Otto::RouteDefinition)

    The route definition

  • otto_instance (Otto) (defaults to: nil)

    The Otto instance for configuration access

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/otto/route_handlers/factory.rb', line 16

def self.create_handler(route_definition, otto_instance = nil)
  # Create base handler based on route kind
  handler_class = case route_definition.kind
                  when :logic then LogicClassHandler
                  when :instance then InstanceMethodHandler
                  when :class then ClassMethodHandler
                  else
                    raise ArgumentError, "Unknown handler kind: #{route_definition.kind}"
                  end

  handler = handler_class.new(route_definition, otto_instance)

  # Always wrap with RouteAuthWrapper to ensure env['otto.strategy_result'] is set
  # - Routes WITH auth requirement: Enforces authentication
  # - Routes WITHOUT auth requirement: Sets anonymous StrategyResult
  if otto_instance&.auth_config
    handler = Otto::Security::Authentication::RouteAuthWrapper.new(
      handler,
      route_definition,
      otto_instance.auth_config,
      otto_instance.security_config
    )
  end

  apply_handler_wrappers(handler, route_definition, otto_instance)
end