Class: DataMapper::Visualizer::GraphViz
- Inherits:
-
Visualization
- Object
- Visualization
- DataMapper::Visualizer::GraphViz
- Defined in:
- lib/dm-visualizer/graphviz.rb
Overview
Visualizes projects by generating a GraphViz diagram.
Instance Attribute Summary collapse
-
#colors ⇒ Object
readonly
The colors to use.
-
#file ⇒ Object
The output file.
-
#format ⇒ Object
The output file format (
:svg
). -
#labels ⇒ Object
readonly
The labels to use.
Attributes inherited from Visualization
#full_names, #naming, #project, #repository_names
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ GraphViz
constructor
Creates a new GraphViz object.
-
#path ⇒ String
The full output path.
-
#visualize ⇒ Object
protected
Generates a GraphViz diagram for a project.
Methods inherited from Visualization
#class_name, #foreign_key_name, #model_name, #model_repository_name, #property_name, #property_type_name, #visualize!
Constructor Details
#initialize(options = {}) ⇒ GraphViz
Creates a new GraphViz object.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/dm-visualizer/graphviz.rb', line 51 def initialize(={}) super() @format = :svg @colors = { :one_to_many => 'blue', :one_to_one => 'red', :inheritence => 'cyan' } @labels = { :one_to_many => '1:m', :one_to_one => '1:1' } if [:file] @file = File.([:file]) end if [:format] @format = [:format].to_sym end if [:colors] [:colors].each do |name,value| @colors[name.to_sym] = value.to_s end end if [:labels] [:labels].each do |name,value| @labels[name.to_sym] = value.to_s end end end |
Instance Attribute Details
#colors ⇒ Object (readonly)
The colors to use
26 27 28 |
# File 'lib/dm-visualizer/graphviz.rb', line 26 def colors @colors end |
#file ⇒ Object
The output file
20 21 22 |
# File 'lib/dm-visualizer/graphviz.rb', line 20 def file @file end |
#format ⇒ Object
The output file format (:svg
)
23 24 25 |
# File 'lib/dm-visualizer/graphviz.rb', line 23 def format @format end |
#labels ⇒ Object (readonly)
The labels to use
29 30 31 |
# File 'lib/dm-visualizer/graphviz.rb', line 29 def labels @labels end |
Instance Method Details
#path ⇒ String
The full output path.
93 94 95 |
# File 'lib/dm-visualizer/graphviz.rb', line 93 def path "#{@file}.#{@format}" end |
#visualize ⇒ Object (protected)
Generates a GraphViz diagram for a project.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/dm-visualizer/graphviz.rb', line 105 def visualize graph = ::GraphViz.new(:G, :type => :digraph) # Create node for each model project.each_model do |model| properties = project.each_property(model).map do |property| "#{property_name(property)}: #{property_type_name(property)}" end foreign_keys = project.each_foreign_key(model).map do |key,value| "#{foreign_key_name(key)}: #{model_name(value)}" end columns = (properties + foreign_keys) label = "{ #{model_name(model)} | #{columns.join("\n")} }" graph.add_nodes( model_name(model), :shape => 'record', :label => label ) end # Connect model nodes together by relationship project.each_relationship do |relationship,model| source_node = graph.get_node(model_name(model)) target_node = graph.get_node(model_name(relationship.target_model)) case relationship when DataMapper::Associations::OneToMany::Relationship graph.add_edges( source_node, target_node, :color => @colors[:one_to_many], :label => " #{@labels[:one_to_many]}" ) when DataMapper::Associations::OneToOne::Relationship graph.add_edges( source_node, target_node, :color => @colors[:one_to_one], :label => " #{@labels[:one_to_one]}" ) end end # Connect model nodes by inheritence project.each_model_inheritence do |model,ancestor| source_node = graph.get_node(model_name(ancestor)) target_node = graph.get_node(model_name(model)) graph.add_edges( source_node, target_node, :color => @colors[:inheritence] ) end graph.output(@format => self.path) end |