Class: DependencyGrapher::Grapher
- Inherits:
-
Object
- Object
- DependencyGrapher::Grapher
- Defined in:
- lib/dependency_grapher/grapher.rb
Instance Method Summary collapse
- #add_edge(dependency) ⇒ Object
-
#add_node(method) ⇒ Object
Add a method to the structure of stored graph.
- #create_clusters(method) ⇒ Object
- #generate_graph_from_dependencies ⇒ Object
- #get_normal_color(count) ⇒ Object
-
#initialize(dependencies) ⇒ Grapher
constructor
A new instance of Grapher.
- #output(format) ⇒ Object
Constructor Details
#initialize(dependencies) ⇒ Grapher
Returns a new instance of Grapher.
6 7 8 9 |
# File 'lib/dependency_grapher/grapher.rb', line 6 def initialize(dependencies) @dependencies = dependencies generate_graph_from_dependencies end |
Instance Method Details
#add_edge(dependency) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/dependency_grapher/grapher.rb', line 32 def add_edge(dependency) = {} normal_color = get_normal_color(dependency.count) [:color] = dependency.flags.include?(:violation) ? :red : normal_color [:label] = dependency.count > 1 ? dependency.count : "" @graph.add_edges(dependency.kaller.id, dependency.receiver.id, ) end |
#add_node(method) ⇒ Object
Add a method to the structure of stored graph. Defined classes are stored as clusters (subgraphs) and method id is used to identify the node
49 50 51 52 53 54 55 56 |
# File 'lib/dependency_grapher/grapher.rb', line 49 def add_node(method) graph = create_clusters(method) # Node = {} [:label] = method.method_id [:tooltip] = method.full_path graph.add_nodes(method.id, ) end |
#create_clusters(method) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/dependency_grapher/grapher.rb', line 58 def create_clusters(method) = {tooltip: " "} if method.types.include?(:service) [:bgcolor] = :azure3 elsif method.types.include?(:framework) [:bgcolor] = :brown end # Iterate over ancestors (eg. Minitest::Unit yields Minitest, # Unit) # This variable is used to reference the immediate parent of the # current graph. Initializes to the root; updates on each iteration. prev_graph = @graph method.ancestors.each_with_index do |klass, i| # Prepending "cluster_" is mandatory according to ruby-graphviz API graph_id = "cluster_" + method.ancestors[0..i].join("_") # add_graph returns an existing graph if it exists subgraph = prev_graph.add_graph(graph_id, ) subgraph[:label] = klass # Update parent for next iteration prev_graph = subgraph end prev_graph end |
#generate_graph_from_dependencies ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/dependency_grapher/grapher.rb', line 15 def generate_graph_from_dependencies # Initialize a directional graph @graph = GraphViz.digraph( :G, type: :digraph, tooltip: " ") # Iterate through dependency set to get edges and methods @dependencies.each do |dependency| # Methods from the dependencies kaller = dependency.kaller receiver = dependency.receiver # Add edges to set #@graph.add_edges(kaller.method_id, receiver.method_id, color: :red) add_node(kaller) add_node(receiver) add_edge(dependency) end end |
#get_normal_color(count) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/dependency_grapher/grapher.rb', line 40 def get_normal_color(count) code = 80 - count * 5 binding.pry if code < 0 code = 0 if code < 0 "gray#{code}" end |
#output(format) ⇒ Object
11 12 13 |
# File 'lib/dependency_grapher/grapher.rb', line 11 def output(format) @graph.output(format) end |