Class: RubyAPI::TrieNode
- Inherits:
-
Object
- Object
- RubyAPI::TrieNode
- Defined in:
- lib/rubyapi/router.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#route ⇒ Object
Returns the value of attribute route.
Instance Method Summary collapse
-
#initialize ⇒ TrieNode
constructor
A new instance of TrieNode.
- #insert(path, route) ⇒ Object
- #search(path) ⇒ Object
Constructor Details
#initialize ⇒ TrieNode
Returns a new instance of TrieNode.
72 73 74 75 |
# File 'lib/rubyapi/router.rb', line 72 def initialize @children = {} @route = nil end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
70 71 72 |
# File 'lib/rubyapi/router.rb', line 70 def children @children end |
#route ⇒ Object
Returns the value of attribute route.
70 71 72 |
# File 'lib/rubyapi/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/rubyapi/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/rubyapi/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 |