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(route, 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
32
33
# File 'lib/hanami/router/trie.rb', line 24

def add(route, to, constraints)
  segments = segments_from(route)
  node = @root

  segments.each do |segment|
    node = node.put(segment)
  end

  node.leaf!(route, to, constraints)
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



37
38
39
40
41
42
43
44
# File 'lib/hanami/router/trie.rb', line 37

def find(path)
  segments = segments_from(path)
  node = @root

  return unless segments.all? { |segment| node = node.get(segment) }

  node.match(path)&.then { |found| [found.to, found.params] }
end