Last active
August 29, 2015 14:01
-
-
Save Quintus/bbc3d703b525ca6ca144 to your computer and use it in GitHub Desktop.
Ruby Class + Mixin graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# | |
# Copyright (c) 2014 Marvin Gülker | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions | |
# are met: | |
# | |
# 1. Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# | |
# 2. Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in the | |
# documentation and/or other materials provided with the distribution. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
# POSSIBILITY OF SUCH DAMAGE. | |
class Module | |
# Return only the modules that are directly included by | |
# this class. Modules included by modules included by | |
# this class are excluded. | |
def directly_included_modules | |
upper_ancestors = module_ancestors.reduce([]){|ary, mod| ary.concat(mod.module_ancestors)} | |
module_ancestors - upper_ancestors | |
end | |
# Return only the modules that are either directly included | |
# by this class or that are included by those modules. Modules | |
# included by superclasses are excluded. | |
def module_ancestors | |
ancestors.drop(1).take_while{|anc| !anc.kind_of?(Class)} | |
end | |
end | |
class Hash | |
def recursive_each(level = 0, &block) | |
each_pair do |k, v| | |
block.call(k, v, level) | |
v.recursive_each(level + 1, &block) | |
end | |
end | |
end | |
tree = {} | |
ObjectSpace.each_object(Class).each do |klass| | |
# All the Errno:: classes make the graph extremely large, | |
# so just collect them under a single name. | |
if klass.to_s.start_with?("Errno::") | |
ary = ["Errno::*"] | |
else | |
ary = [klass.to_s] | |
end | |
while klass = klass.superclass | |
ary.unshift(klass.to_s) | |
end | |
hsh = tree | |
while k = ary.shift | |
hsh[k] ||= {} | |
hsh[k][ary.first] ||= {} unless ary.empty? | |
hsh = hsh[k] | |
end | |
end | |
def generate_modtree(mod, modtree = {}) | |
modtree[mod] ||= {} | |
mod.directly_included_modules.each do |imod| | |
generate_modtree(imod, modtree[mod]) | |
end | |
end | |
modtree = {} | |
ObjectSpace.each_object(Class).each do |klass| | |
generate_modtree(klass, modtree) | |
end | |
File.open("inheritance.gv", "w") do |file| | |
file.puts "digraph inheritance {" | |
# Draw the inheritance | |
tree.recursive_each do |k, v, level| | |
v.keys.each do |name| | |
file.puts("\"#{k}\" -> \"#{name}\";") | |
end | |
end | |
# Draw the includes | |
modtree.recursive_each do |mod, included_mods, level| | |
file.puts "\"#{mod}\" [shape=box,color=blue];" unless mod.kind_of?(Class) | |
included_mods.keys.each do |included_mod| | |
file.puts "\"#{included_mod}\" -> \"#{mod}\" [color=blue];" | |
end | |
end | |
file.puts "}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment