Class: Hanami::Router::Trie Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Trie data structure to store routes

Since:

  • 2.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTrie

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Trie.

Since:

  • 2.0.0



18
19
20
# File 'lib/hanami/router/trie.rb', line 18

def initialize
  @root = Node.new
end

Instance Attribute Details

#rootObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



14
15
16
# File 'lib/hanami/router/trie.rb', line 14

def root
  @root
end

Instance Method Details

#add(path, to, constraints) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



24
25
26
27
28
29
30
31
# File 'lib/hanami/router/trie.rb', line 24

def add(path, to, constraints)
  node = @root
  for_each_segment(path) do |segment|
    node = node.put(segment, constraints)
  end

  node.leaf!(to)
end

#find(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hanami/router/trie.rb', line 35

def find(path)
  node = @root
  params = {}

  for_each_segment(path) do |segment|
    break unless node

    child, captures = node.get(segment)
    params.merge!(captures) if captures

    node = child
  end

  return [node.to, params] if node&.leaf?

  nil
end