Class: RubyRoutes::Node
- Inherits:
-
Object
- Object
- RubyRoutes::Node
- Defined in:
- lib/ruby_routes/node.rb
Instance Attribute Summary collapse
-
#dynamic_child ⇒ Object
Returns the value of attribute dynamic_child.
-
#handlers ⇒ Object
Returns the value of attribute handlers.
-
#is_endpoint ⇒ Object
Returns the value of attribute is_endpoint.
-
#param_name ⇒ Object
Pre-cache param names as strings to avoid repeated .to_s calls.
-
#static_children ⇒ Object
Returns the value of attribute static_children.
-
#wildcard_child ⇒ Object
Returns the value of attribute wildcard_child.
Instance Method Summary collapse
-
#add_handler(method, handler) ⇒ Object
Normalize method once and cache string keys.
- #get_handler(method) ⇒ Object
-
#initialize ⇒ Node
constructor
A new instance of Node.
-
#traverse_for(segment, index, segments, params) ⇒ Object
Fast traversal: minimal allocations, streamlined branching Returns [next_node_or_nil, should_break_bool] or [nil, false] if no match.
Constructor Details
#initialize ⇒ Node
Returns a new instance of Node.
8 9 10 11 12 13 14 15 |
# File 'lib/ruby_routes/node.rb', line 8 def initialize @static_children = {} @dynamic_child = nil @wildcard_child = nil @handlers = {} @param_name = nil @is_endpoint = false end |
Instance Attribute Details
#dynamic_child ⇒ Object
Returns the value of attribute dynamic_child.
5 6 7 |
# File 'lib/ruby_routes/node.rb', line 5 def dynamic_child @dynamic_child end |
#handlers ⇒ Object
Returns the value of attribute handlers.
5 6 7 |
# File 'lib/ruby_routes/node.rb', line 5 def handlers @handlers end |
#is_endpoint ⇒ Object
Returns the value of attribute is_endpoint.
5 6 7 |
# File 'lib/ruby_routes/node.rb', line 5 def is_endpoint @is_endpoint end |
#param_name ⇒ Object
Pre-cache param names as strings to avoid repeated .to_s calls
45 46 47 |
# File 'lib/ruby_routes/node.rb', line 45 def param_name @param_name end |
#static_children ⇒ Object
Returns the value of attribute static_children.
5 6 7 |
# File 'lib/ruby_routes/node.rb', line 5 def static_children @static_children end |
#wildcard_child ⇒ Object
Returns the value of attribute wildcard_child.
5 6 7 |
# File 'lib/ruby_routes/node.rb', line 5 def wildcard_child @wildcard_child end |
Instance Method Details
#add_handler(method, handler) ⇒ Object
Normalize method once and cache string keys
55 56 57 58 59 |
# File 'lib/ruby_routes/node.rb', line 55 def add_handler(method, handler) method_key = method.to_s.upcase @handlers[method_key] = handler @is_endpoint = true end |
#get_handler(method) ⇒ Object
61 62 63 |
# File 'lib/ruby_routes/node.rb', line 61 def get_handler(method) @handlers[method] # assume already normalized upstream end |
#traverse_for(segment, index, segments, params) ⇒ Object
Fast traversal: minimal allocations, streamlined branching Returns [next_node_or_nil, should_break_bool] or [nil, false] if no match.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ruby_routes/node.rb', line 19 def traverse_for(segment, index, segments, params) # Static match: O(1) hash lookup child = @static_children[segment] return [child, false] if child # Dynamic match: single segment capture if (dyn = @dynamic_child) params[dyn.param_name] = segment if params return [dyn, false] end # Wildcard match: consume remainder (last resort) if (wild = @wildcard_child) if params # Build remainder path without intermediate array allocation remainder = segments[index..-1] params[wild.param_name] = remainder.size == 1 ? remainder[0] : remainder.join('/') end return [wild, true] end # No match [nil, false] end |