Class: Liberty::Router::Trie

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

Constant Summary collapse

PATH_SEPARATOR =
"/"

Instance Method Summary collapse

Constructor Details

#initializeTrie

Returns a new instance of Trie.



10
11
12
# File 'lib/liberty/router/trie.rb', line 10

def initialize
  @root = Node.new
end

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

Yields:

  • (route_params)


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


29
30
31
# File 'lib/liberty/router/trie.rb', line 29

def print(verb, routes)
  @root.print(verb, routes)
end