Class: RubyRoutes::RouteSet

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_routes/route_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouteSet

Returns a new instance of RouteSet.



5
6
7
8
9
10
11
12
13
14
# File 'lib/ruby_routes/route_set.rb', line 5

def initialize
  @tree = RubyRoutes::RadixTree.new
  @named_routes = {}
  @routes = []
  # Optimized recognition cache with better data structures
  @recognition_cache = {}
  @cache_hits = 0
  @cache_misses = 0
  @recognition_cache_max = 8192  # larger for better hit rates
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



3
4
5
# File 'lib/ruby_routes/route_set.rb', line 3

def routes
  @routes
end

Instance Method Details

#add_route(route) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/ruby_routes/route_set.rb', line 16

def add_route(route)
  @routes << route
  @tree.add(route.path, route.methods, route)
  @named_routes[route.name] = route if route.named?
  # Clear recognition cache when routes change
  @recognition_cache.clear if @recognition_cache.size > 100
  route
end

#cache_statsObject

Performance monitoring



125
126
127
128
129
130
131
132
133
134
# File 'lib/ruby_routes/route_set.rb', line 125

def cache_stats
  total = @cache_hits + @cache_misses
  hit_rate = total > 0 ? (@cache_hits.to_f / total * 100).round(2) : 0
  {
    hits: @cache_hits,
    misses: @cache_misses,
    hit_rate: "#{hit_rate}%",
    size: @recognition_cache.size
  }
end

#clear!Object



98
99
100
101
102
103
104
# File 'lib/ruby_routes/route_set.rb', line 98

def clear!
  @routes.clear
  @named_routes.clear
  @recognition_cache.clear
  @tree = RadixTree.new
  @cache_hits = @cache_misses = 0
end

#each(&block) ⇒ Object



115
116
117
118
# File 'lib/ruby_routes/route_set.rb', line 115

def each(&block)
  return enum_for(:each) unless block_given?
  @routes.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/ruby_routes/route_set.rb', line 111

def empty?
  @routes.empty?
end

#find_named_route(name) ⇒ Object

Raises:



32
33
34
35
36
# File 'lib/ruby_routes/route_set.rb', line 32

def find_named_route(name)
  route = @named_routes[name]
  return route if route
  raise RouteNotFound, "No route named '#{name}'"
end

#find_route(request_method, request_path) ⇒ Object



25
26
27
28
29
30
# File 'lib/ruby_routes/route_set.rb', line 25

def find_route(request_method, request_path)
  # Optimized: avoid repeated string allocation
  method_up = request_method.to_s.upcase
  handler, _params = @tree.find(request_path, method_up)
  handler
end

#generate_path(name, params = {}) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/ruby_routes/route_set.rb', line 85

def generate_path(name, params = {})
  route = @named_routes[name]
  if route
    route.generate_path(params)
  else
    raise RouteNotFound, "No route named '#{name}'"
  end
end

#generate_path_from_route(route, params = {}) ⇒ Object



94
95
96
# File 'lib/ruby_routes/route_set.rb', line 94

def generate_path_from_route(route, params = {})
  route.generate_path(params)
end

#include?(route) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/ruby_routes/route_set.rb', line 120

def include?(route)
  @routes.include?(route)
end

#match(request_method, request_path) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ruby_routes/route_set.rb', line 38

def match(request_method, request_path)
  # Fast path: normalize method once
  method_up = method_lookup(request_method)

  # Optimized cache key: avoid string interpolation when possible
  cache_key = build_cache_key(method_up, request_path)

  # Cache hit: return immediately (cached result includes full structure)
  if (cached_result = @recognition_cache[cache_key])
    @cache_hits += 1
    return cached_result
  end

  @cache_misses += 1

  # Use thread-local params to avoid allocations
  params = get_thread_local_params
  handler, _ = @tree.find(request_path, method_up, params)
  return nil unless handler

  route = handler

  # Fast path: merge defaults only if they exist
  merge_defaults(route, params) if route.defaults && !route.defaults.empty?

  # Fast path: parse query params only if needed
  if request_path.include?('?')
    merge_query_params(route, request_path, params)
  end

  # Create return hash and cache the complete result
  result_params = params.dup
  result = {
    route: route,
    params: result_params,
    controller: route.controller,
    action: route.action
  }.freeze

  insert_cache_entry(cache_key, result)
  result
end

#recognize_path(path, method = :get) ⇒ Object



81
82
83
# File 'lib/ruby_routes/route_set.rb', line 81

def recognize_path(path, method = :get)
  match(method, path)
end

#sizeObject Also known as: length



106
107
108
# File 'lib/ruby_routes/route_set.rb', line 106

def size
  @routes.size
end