Class: Liberty::Router::Trie
- Inherits:
-
Object
- Object
- Liberty::Router::Trie
- Defined in:
- lib/liberty/router/trie.rb
Constant Summary collapse
- PATH_SEPARATOR =
"/"
Instance Method Summary collapse
- #add_route(path, endpoint) ⇒ Object
- #find_route(path) {|route_params| ... } ⇒ Object
-
#initialize ⇒ Trie
constructor
A new instance of Trie.
- #print(verb, routes) ⇒ Object
Constructor Details
Instance Method Details
#add_route(path, endpoint) ⇒ Object
14 15 16 |
# File 'lib/liberty/router/trie.rb', line 14 def add_route(path, endpoint) traverse(path) { |segment, node| node.add(segment) }.attach(endpoint) end |
#find_route(path) {|route_params| ... } ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/liberty/router/trie.rb', line 18 def find_route(path) route_params = {} node = traverse(path) do |segment, node| node.find(segment) { |params| route_params.merge!(params) } end yield(route_params) if node && block_given? node&.endpoint end |
#print(verb, routes) ⇒ Object
29 30 31 |
# File 'lib/liberty/router/trie.rb', line 29 def print(verb, routes) @root.print(verb, routes) end |