Class: RubyRoutes::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_routes/route.rb,
lib/ruby_routes/route/small_lru.rb

Defined Under Namespace

Classes: SmallLru

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Route



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_routes/route.rb', line 11

def initialize(path, options = {})
  @path = normalize_path(path)
  # Pre-normalize and freeze methods at creation time
  raw_methods = Array(options[:via] || :get)
  @methods = raw_methods.map { |m| normalize_method(m) }.freeze
  @methods_set = @methods.to_set.freeze
  @controller = extract_controller(options)
  @action = options[:action] || extract_action(options[:to])
  @name = options[:as]
  @constraints = options[:constraints] || {}
  # Pre-normalize defaults to string keys and freeze
  @defaults = (options[:defaults] || {}).transform_keys(&:to_s).freeze

  # Pre-compile everything at initialization
  precompile_route_data
  validate_route!
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



9
10
11
# File 'lib/ruby_routes/route.rb', line 9

def action
  @action
end

#constraintsObject (readonly)

Returns the value of attribute constraints.



9
10
11
# File 'lib/ruby_routes/route.rb', line 9

def constraints
  @constraints
end

#controllerObject (readonly)

Returns the value of attribute controller.



9
10
11
# File 'lib/ruby_routes/route.rb', line 9

def controller
  @controller
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



9
10
11
# File 'lib/ruby_routes/route.rb', line 9

def defaults
  @defaults
end

#methodsObject (readonly)

Returns the value of attribute methods.



9
10
11
# File 'lib/ruby_routes/route.rb', line 9

def methods
  @methods
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/ruby_routes/route.rb', line 9

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/ruby_routes/route.rb', line 9

def path
  @path
end

Instance Method Details

#collection?Boolean



52
53
54
# File 'lib/ruby_routes/route.rb', line 52

def collection?
  !@is_resource
end

#extract_params(request_path, parsed_qp = nil) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/ruby_routes/route.rb', line 35

def extract_params(request_path, parsed_qp = nil)
  path_params = extract_path_params_fast(request_path)

  return EMPTY_HASH unless path_params

  # Use optimized param building
  build_params_hash(path_params, request_path, parsed_qp)
end

#generate_path(params = {}) ⇒ Object

Optimized path generation with better caching and fewer allocations



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruby_routes/route.rb', line 61

def generate_path(params = {})
  return ROOT_PATH if @path == ROOT_PATH

  # Fast path: empty params and no required params
  if params.empty? && @required_params.empty?
    return @static_path if @static_path
  end

  # Build merged params efficiently
  merged = build_merged_params(params)

  # Check required params (fast Set operation)
  missing_params = @required_params_set - merged.keys
  unless missing_params.empty?
    raise RubyRoutes::RouteNotFound, "Missing params: #{missing_params.to_a.join(', ')}"
  end

  # Check for nil values in required params
  nil_params = @required_params_set.select { |param| merged[param].nil? }
  unless nil_params.empty?
    raise RubyRoutes::RouteNotFound, "Missing or nil params: #{nil_params.to_a.join(', ')}"
  end

  # Cache lookup
  cache_key = build_cache_key_fast(merged)
  if (cached = @gen_cache.get(cache_key))
    return cached
  end

  # Generate path using string buffer (avoid array allocations)
  path_str = generate_path_string(merged)
  @gen_cache.set(cache_key, path_str)
  path_str
end

#match?(request_method, request_path) ⇒ Boolean



29
30
31
32
33
# File 'lib/ruby_routes/route.rb', line 29

def match?(request_method, request_path)
  # Fast method check: use frozen Set for O(1) lookup
  return false unless @methods_set.include?(request_method.to_s.upcase)
  !!extract_path_params_fast(request_path)
end

#named?Boolean



44
45
46
# File 'lib/ruby_routes/route.rb', line 44

def named?
  !@name.nil?
end

#parse_query_params(path) ⇒ Object



56
57
58
# File 'lib/ruby_routes/route.rb', line 56

def parse_query_params(path)
  query_params_fast(path)
end

#query_params(request_path) ⇒ Object

Fast query params method (cached and optimized)



97
98
99
# File 'lib/ruby_routes/route.rb', line 97

def query_params(request_path)
  query_params_fast(request_path)
end

#resource?Boolean



48
49
50
# File 'lib/ruby_routes/route.rb', line 48

def resource?
  @is_resource
end