Class: Depdump::Registry::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/depdump/registry/tree.rb,
lib/depdump/registry/tree/node.rb,
lib/depdump/registry/tree/relation.rb

Defined Under Namespace

Classes: Node, Relation

Instance Method Summary collapse

Instance Method Details

#each_nodeObject



21
22
23
24
# File 'lib/depdump/registry/tree.rb', line 21

def each_node
  return unless block_given?
  root.each { |node| yield node }
end

#find_or_create_node(namespaces, parent) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/depdump/registry/tree.rb', line 11

def find_or_create_node(namespaces, parent)
  # TODO: could be cached rather than search everytime
  registered = root.detect { |node| namespaces == node.namespaces }
  return registered if registered

  Node.new(namespaces: namespaces, parent: parent).tap { |n|
    parent.children << n
  }
end

#resolve(partial_namespaces, entry_node) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/depdump/registry/tree.rb', line 26

def resolve(partial_namespaces, entry_node)
  current_node = entry_node
  resolved_node = nil

  while current_node && resolved_node.nil?
    resolved_node = current_node.dig(partial_namespaces)
    current_node = current_node&.parent
  end

  resolved_node
end

#rootObject



4
5
6
7
8
9
# File 'lib/depdump/registry/tree.rb', line 4

def root
  @root ||= Node.new(
    namespaces: [],
    parent: nil,
  )
end