Class: DataMapper::Visualizer::GraphViz

Inherits:
Visualization show all
Defined in:
lib/dm-visualizer/graphviz.rb

Overview

Visualizes projects by generating a GraphViz diagram.

Instance Attribute Summary collapse

Attributes inherited from Visualization

#full_names, #naming, #project, #repository_names

Instance Method Summary collapse

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.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :file (String)

    The output file path.

  • :format (Symbol) — default: :svg

    The format of the generated graph.

  • :colors (Hash)

    The colors to use for :one_to_many and :one_to_one relationship edges.

  • :labels (Hash)

    The labels to use for :one_to_many and :one_to_one relationship edges.



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(options={})
  super(options)

  @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 options[:file]
    @file = File.expand_path(options[:file])
  end

  if options[:format]
    @format = options[:format].to_sym
  end

  if options[:colors]
    options[:colors].each do |name,value|
      @colors[name.to_sym] = value.to_s
    end
  end

  if options[:labels]
    options[:labels].each do |name,value|
      @labels[name.to_sym] = value.to_s
    end
  end
end

Instance Attribute Details

#colorsObject (readonly)

The colors to use



26
27
28
# File 'lib/dm-visualizer/graphviz.rb', line 26

def colors
  @colors
end

#fileObject

The output file



20
21
22
# File 'lib/dm-visualizer/graphviz.rb', line 20

def file
  @file
end

#formatObject

The output file format (:svg)



23
24
25
# File 'lib/dm-visualizer/graphviz.rb', line 23

def format
  @format
end

#labelsObject (readonly)

The labels to use



29
30
31
# File 'lib/dm-visualizer/graphviz.rb', line 29

def labels
  @labels
end

Instance Method Details

#pathString

The full output path.

Returns:

  • (String)

    The full output path, including file extension.



93
94
95
# File 'lib/dm-visualizer/graphviz.rb', line 93

def path
  "#{@file}.#{@format}"
end

#visualizeObject (protected)

Generates a GraphViz diagram for a project.

Parameters:

  • path (String)

    The path to save the graph image to.



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