1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 52 53 54 55 56 57 58 59 60
|
require 'twitter'
COLON = ':'.freeze
UNDERSCORE = '_'.freeze
TAB = "\t".freeze
NAMESPACE = 'Twitter::'.freeze
# Colons are invalid characters in DOT nodes.
# Replace them with underscores.
# http://www.graphviz.org/doc/info/lang.html
def nodize(klass)
klass.name.tr(COLON, UNDERSCORE)
end
nodes = {}
edges = {}
twitter_objects = ObjectSpace.each_object(Class).select do |klass|
klass.name.to_s.start_with?(NAMESPACE)
end
twitter_objects.each do |klass|
loop do
unless klass.nil? || klass.superclass.nil? || klass.name.empty?
nodes[nodize(klass)] = klass.name
edges[nodize(klass)] = nodize(klass.superclass)
end
klass = klass.superclass
break if klass.nil?
end
end
edges.delete(nil)
@indent = 0
def indent
@indent += 1
yield
@indent -= 1
end
def puts(string)
super(TAB * @indent + string)
end
puts 'digraph classes {'
# Add or remove DOT formatting options here
indent do
puts 'graph [rotate=0, rankdir="LR"]'
puts 'node [fillcolor="#c4ddec", style="filled", fontname="Helvetica Neue"]'
puts 'edge [color="#444444"]'
nodes.sort.each do |node, label|
puts "#{node} [label=\"#{label}\"]"
end
edges.sort.each do |child, parent|
puts "#{child} -> #{parent}"
end
end
puts '}'
|