Class: Takagi::Router::RouteMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/takagi/router/route_matcher.rb

Overview

Handles dynamic route matching with parameter extraction.

Extracted from Router to follow Single Responsibility Principle. Manages matching URL patterns with dynamic segments (e.g., /users/:id) and extracting parameters from matched routes.

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ RouteMatcher

Returns a new instance of RouteMatcher.

Parameters:

  • logger (Logger)

    Logger instance for debugging



12
13
14
# File 'lib/takagi/router/route_matcher.rb', line 12

def initialize(logger)
  @logger = logger
end

Instance Method Details

#match(routes, method, path) ⇒ Array(RouteEntry, Hash), Array(nil, Hash)

Matches dynamic routes that contain parameters (e.g., ‘/users/:id`)

Parameters:

  • routes (Hash)

    Map of route keys to RouteEntry objects

  • method (String)

    HTTP method

  • path (String)

    Request path

Returns:

  • (Array(RouteEntry, Hash), Array(nil, Hash))

    Matched route entry and parameters, or [nil, {}]



22
23
24
25
26
27
28
# File 'lib/takagi/router/route_matcher.rb', line 22

def match(routes, method, path)
  matched_route = locate_dynamic_route(routes, method, path)
  return matched_route if matched_route

  @logger.debug 'No route matched!'
  [nil, {}]
end