Class: Takagi::Router::RouteContext

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Helpers
Defined in:
lib/takagi/router.rb

Overview

Provides the execution context for route handlers, exposing helper methods for configuring CoRE Link Format attributes via a small DSL.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#halt, #json, #respond, #validate_params

Constructor Details

#initialize(entry, request, params, receiver) ⇒ RouteContext

Returns a new instance of RouteContext.



87
88
89
90
91
92
93
94
95
# File 'lib/takagi/router.rb', line 87

def initialize(entry, request, params, receiver)
  @entry = entry
  @request = request
  @params = params
  @receiver = receiver
  # Create a fresh AttributeSet for this request to avoid cross-request state sharing
  # Initialize it with a copy of the entry's current metadata
  @core_attributes = Core::AttributeSet.new(@entry..dup)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object (private)

Delegates method calls to the receiver (application instance) This allows route handlers to call application methods within their blocks Example: get ‘/users’ do; fetch_users; end - calls application’s fetch_users method



130
131
132
133
134
135
136
# File 'lib/takagi/router.rb', line 130

def method_missing(name, ...)
  if @receiver.respond_to?(name)
    @receiver.public_send(name, ...)
  else
    super
  end
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



76
77
78
# File 'lib/takagi/router.rb', line 76

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



76
77
78
# File 'lib/takagi/router.rb', line 76

def request
  @request
end

Instance Method Details

#run(block) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/takagi/router.rb', line 97

def run(block)
  return unless block

  args = case block.arity
         when 0 then []
         when 1 then [request]
         else
           [request, params]
         end
  args = [request, params] if block.arity.negative?

  # Support halt for early returns
  result = catch(:halt) do
    instance_exec(*args, &block)
  end

  result
ensure
  @core_attributes.apply!
end