Module: CallGrapher

Defined in:
lib/callgrapher.rb

Class Method Summary collapse

Class Method Details

.make_graph(graph, path) ⇒ Object

Parameters:

  • graph

    a string containing a dot format digraph



72
73
74
# File 'lib/callgrapher.rb', line 72

def self.make_graph(graph, path)
  IO.popen("dot -Tpng -o#{path}", 'w') { |out| out.write graph }
end

.make_graphviz_graph(call_graph) ⇒ Object

Parameters:

  • call_graph (Hash<Class, Set>)

    the graph to transform into a Graphviz digraph



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/callgrapher.rb', line 55

def self.make_graphviz_graph(call_graph)

  graph = ''
  graph << 'digraph callgraph {'

  call_graph.each do |klass, dependencies|
    graph << "\"#{klass}\" [shape=box];\n"
    dependencies.each do |dependency|
      graph << "\"#{dependency}\" [shape=box];\n"
      graph << "\"#{klass}\" -> \"#{dependency}\";\n"
    end
  end

  graph << '}'
end

.trace_class_dependencies(file_whitelist = nil, class_blacklist = nil, class_name_processor = lambda{|x| x}) ⇒ Object

Parameters:

  • file_whitelist (Array<String>) (defaults to: nil)

    Only include classes defined in these files.

  • class_blacklist (Array<String>) (defaults to: nil)

    Don’t include these classes.

  • class_name_processor (Proc) (defaults to: lambda{|x| x})

    A Proc that takes a single argument (a fully qualified classname) and returns the class as it should be shown in the graph.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/callgrapher.rb', line 27

def self.trace_class_dependencies(file_whitelist = nil,
                                  class_blacklist = nil,
                                  class_name_processor = lambda{|x| x})
  callstack = []
  classgraph = Hash.new{ |hash, key| hash[key] = Set.new }

  set_trace_func proc{ |event, file, line, id, binding, klass|
    next if file_whitelist && !file_whitelist.include?(file)
    next if class_blacklist && class_blacklist.include?(klass.to_s)
    case event
      when 'call'
        caller = callstack[-1]
        klass = class_name_processor.call klass.name
        classgraph[caller].add klass if caller && caller != klass
        callstack.push klass
      when 'return'
        callstack.pop
    end
  }

  yield

  set_trace_func nil
  classgraph
end