Class: DepsGrapher::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/deps_grapher/graph.rb

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



9
10
11
# File 'lib/deps_grapher/graph.rb', line 9

def initialize
  @graph = {}
end

Instance Method Details

#add_edge(edge) ⇒ Object



13
14
15
16
# File 'lib/deps_grapher/graph.rb', line 13

def add_edge(edge)
  @graph[edge.from] ||= []
  @graph[edge.from] << edge.to
end

#find_path(source_path_matcher, target_path_matcher) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/deps_grapher/graph.rb', line 18

def find_path(source_path_matcher, target_path_matcher)
  paths = []
  visited = {}

  @graph.each_key do |start_node|
    next if source_path_matcher && !source_path_matcher.match?(start_node.class_name)

    dfs start_node, target_path_matcher, visited, [start_node], paths
  end

  nodes = Set.new
  edges = Set.new

  paths.each do |path|
    collect! path, nodes, edges
  end

  [nodes, edges]
end