Class: Depdump::DependencyGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/depdump/dependency_graph.rb

Defined Under Namespace

Modules: Formatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree) ⇒ DependencyGraph

Returns a new instance of DependencyGraph.



7
8
9
10
11
# File 'lib/depdump/dependency_graph.rb', line 7

def initialize(tree)
  @nodes = Set.new
  @edges = Set.new
  build(tree)
end

Instance Attribute Details

#edgesObject (readonly)

Returns the value of attribute edges.



5
6
7
# File 'lib/depdump/dependency_graph.rb', line 5

def edges
  @edges
end

#nodesObject (readonly)

Returns the value of attribute nodes.



5
6
7
# File 'lib/depdump/dependency_graph.rb', line 5

def nodes
  @nodes
end

Instance Method Details

#build(tree) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/depdump/dependency_graph.rb', line 13

def build(tree)
  tree.each_node do |node|
    next if node == tree.root

    @nodes << node
    node.relations.each do |r|
      referenced_namespaces = r.resolve(tree)
      if referenced_namespaces
        @edges << { from: node.namespaces, to: referenced_namespaces }
      else
        # TODO: Show file path and line no.
        warn "[skip] cannot resolve: #{node.namespaces} => #{r.reference}"
      end
    end
  end
end

#formatObject



30
31
32
# File 'lib/depdump/dependency_graph.rb', line 30

def format
  Depdump.config.formatter.call(nodes, edges)
end