Class: RubyAPI::TrieNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTrieNode

Returns a new instance of TrieNode.



72
73
74
75
# File 'lib/fastrb/router.rb', line 72

def initialize
  @children = {}
  @route = nil
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



70
71
72
# File 'lib/fastrb/router.rb', line 70

def children
  @children
end

#routeObject

Returns the value of attribute route.



70
71
72
# File 'lib/fastrb/router.rb', line 70

def route
  @route
end

Instance Method Details

#insert(path, route) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/fastrb/router.rb', line 77

def insert(path, route)
  segments = path.split("/").reject(&:empty?)
  node = self
  segments.each do |seg|
    child_key = seg.start_with?(":") ? :__param__ : seg
    node.children[child_key] ||= TrieNode.new
    node = node.children[child_key]
  end
  node.route = route
end

#search(path) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/fastrb/router.rb', line 88

def search(path)
  segments = path.split("/").reject(&:empty?)
  node = self
  segments.each do |seg|
    child = node.children[seg] || node.children[:__param__]
    return nil unless child
    node = child
  end
  node.route
end