class Rage::Router::StaticNode
def compile_prefix_match
def compile_prefix_match if @prefix.length == 1 @match_prefix = ->(_, _) { true } return end lines = (1...@prefix.length).map do |i| "path[i + #{i}]&.ord == #{@prefix[i].ord}" end @match_prefix = eval("->(path, i) { #{lines.join(" && ")} }") end
def create_parametric_child(static_suffix, node_path)
def create_parametric_child(static_suffix, node_path) parametric_child = @parametric_children[0] if parametric_child parametric_child.node_paths.add(node_path) return parametric_child end parametric_child = ParametricNode.new(static_suffix, node_path) @parametric_children << parametric_child @parametric_children.sort! do |child1, child2| if child1.static_suffix.nil? 1 elsif child2.static_suffix.nil? -1 elsif child2.static_suffix.end_with?(child1.static_suffix) 1 elsif child1.static_suffix.end_with?(child2.static_suffix) -1 else 0 end end parametric_child end
def create_wildcard_child
def create_wildcard_child @wildcard_child ||= WildcardNode.new end
def get_next_node(path, path_index, node_stack, params_count)
def get_next_node(path, path_index, node_stack, params_count) node = find_static_matching_child(path, path_index) parametric_brother_node_index = 0 unless node return @wildcard_child if @parametric_children.empty? node = @parametric_children[0] parametric_brother_node_index = 1 end if @wildcard_child node_stack << { params_count: params_count, brother_path_index: path_index, brother_node: @wildcard_child } end i = @parametric_children.length - 1 while i >= parametric_brother_node_index node_stack << { params_count: params_count, brother_path_index: path_index, brother_node: @parametric_children[i] } i -= 1 end node end
def initialize(prefix)
def initialize(prefix) super() @prefix = prefix @wildcard_child = nil @parametric_children = [] @kind = Node::STATIC compile_prefix_match end
def split(parent_node, length)
def split(parent_node, length) parent_prefix = @prefix[0, length] child_prefix = @prefix[length, @prefix.length - length] @prefix = child_prefix compile_prefix_match static_node = StaticNode.new(parent_prefix) static_node.static_children[child_prefix[0]] = self parent_node.static_children[parent_prefix[0]] = static_node static_node end