Created
May 12, 2010 20:03
-
-
Save dsisnero/399065 to your computer and use it in GitHub Desktop.
Graph of data mapper
This file contains hidden or 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
| #!/usr/bin/env ruby -Ku | |
| # encoding: utf-8 | |
| require 'rubygems' | |
| require 'dm-core' | |
| require 'graphviz' | |
| def recursive_require(dir) | |
| Dir.glob("#{dir}/**/*.rb").sort.each { |file| require file } | |
| end | |
| def gen_dot | |
| g = GraphViz.new(:G, :type => :digraph) | |
| # Create node for each model | |
| DataMapper::Model.descendants.each do |model| | |
| properties = model.properties.map do |property| | |
| "#{property.name}: #{Extlib::Inflection.demodulize(property.type.name)}" | |
| end | |
| label = "{ #{model.name} | #{properties.join("\n")} }" | |
| g.add_node(model.name, :shape => 'record', :label => label) | |
| end | |
| # Connect nodes together | |
| DataMapper::Model.descendants.each do |model| | |
| source_node = g.get_node(model.name) | |
| model.relationships.each_value do |relationship| | |
| next if relationship.respond_to?(:through) | |
| target_node = g.get_node(relationship.target_model.name) | |
| case relationship | |
| when DataMapper::Associations::OneToMany::Relationship | |
| g.add_edge(source_node, target_node, :color => 'blue', :label => ' 1:m') | |
| when DataMapper::Associations::OneToOne::Relationship | |
| g.add_edge(source_node, target_node, :color => 'red', :label => ' 1:1') | |
| end | |
| end | |
| end | |
| g.output #(:png => 'graph.png') | |
| end | |
| def run(dir) | |
| recursive_require(dir) | |
| gen_dot | |
| end | |
| if __FILE__ == $0 | |
| if ARGV.empty? | |
| puts "Usage: #{__FILE__} dir" | |
| exit | |
| end | |
| run(ARGV.first) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment