Class: Dige::GraphvizDiagram

Inherits:
Object
  • Object
show all
Includes:
ClassBase
Defined in:
lib/dige/graphviz_diagram.rb

Direct Known Subclasses

EntityRelationshipDiagram, UMLClassDiagram

Defined Under Namespace

Classes: EntityRelationshipDiagram, UMLClassDiagram

Constant Summary collapse

DEFAULT_NODE_WIDTH =
3.0
DEFAULT_NODE_HEIGHT =
2.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassBase

#initialize

Instance Attribute Details

#associationsObject

Returns the value of attribute associations.



3
4
5
# File 'lib/dige/graphviz_diagram.rb', line 3

def associations
  @associations
end

#klassesObject

Returns the value of attribute klasses.



3
4
5
# File 'lib/dige/graphviz_diagram.rb', line 3

def klasses
  @klasses
end

#parent_child_relationsObject

Returns the value of attribute parent_child_relations.



3
4
5
# File 'lib/dige/graphviz_diagram.rb', line 3

def parent_child_relations
  @parent_child_relations
end

Instance Method Details

#debug_inspectObject



73
74
75
76
77
78
79
# File 'lib/dige/graphviz_diagram.rb', line 73

def debug_inspect
  [
    klasses.debug_inspect,
    associations.debug_inspect,
    parent_child_relations.debug_inspect
  ].flatten
end

#edge(from, to, directed = true, options = {}) ⇒ Object



64
65
66
67
# File 'lib/dige/graphviz_diagram.rb', line 64

def edge(from, to, directed=true, options={})
  edge_options = options.empty? ? "" : "[" + options.collect { |k,v| "#{k}=#{quote(v)}" }.join(",") + "]"
  "#{quote(from)} #{directed ? "->" : "--"} #{quote(to)}#{edge_options};"
end

#edge_settingsObject



49
50
51
52
53
54
# File 'lib/dige/graphviz_diagram.rb', line 49

def edge_settings
  {
    :arrowsize => 1.0,
    :penwidth => 2.0
  }
end

#generate_diagram(file = nil, type = 'png', graphtype = :digraph, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dige/graphviz_diagram.rb', line 8

def generate_diagram(file = nil, type = 'png', graphtype = :digraph, &block)
  output = [header(*([graphtype, file].compact)), settings, yield, "}"].flatten.compact.join("\n")
  puts output if ENV['DEBUG'] and ENV['DEBUG'].upcase == 'TRUE'
  
  unless file.nil?
    File.open("#{file}.dot", "w") do |f| f.write output end
    unless system("dot -T#{type} -o#{file}.#{type} #{file}.dot") && ((ENV['NO_RM'] && ENV['NO_RM'].upcase == 'TRUE') || FileUtils.rm_f("#{file}.dot"))
      $stderr.puts "error: failed to generate png diagram from dot file #{file}.dot, file is left existing for investigation."
    end
  else
    puts output
  end
end

#graph_settingsObject



34
35
36
37
38
39
40
# File 'lib/dige/graphviz_diagram.rb', line 34

def graph_settings
  {
    :outputorder => :nodesfirst,
    :concentrate => true,
    :ranksep => 2.5
  }
end

#header(type, name = "my_diagram") ⇒ Object



22
23
24
# File 'lib/dige/graphviz_diagram.rb', line 22

def header(type, name = "my_diagram")
  "#{type} #{name} {"
end

#node(name, options = {}) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/dige/graphviz_diagram.rb', line 56

def node(name, options={})
  associations_count = options.delete(:associations_count)
  options[:width] = DEFAULT_NODE_WIDTH
  options[:height] = DEFAULT_NODE_HEIGHT
  node_options = options.empty? ? "" : "[" + options.collect { |k,v| "#{k}=#{quote(v)}" }.join(",") + "]"
  "#{quote(name)}#{node_options};"
end

#node_settingsObject



42
43
44
45
46
47
# File 'lib/dige/graphviz_diagram.rb', line 42

def node_settings
  {
    :shape => :box,
    :fontsize => 36.0
  }
end

#quote(string) ⇒ Object



69
70
71
# File 'lib/dige/graphviz_diagram.rb', line 69

def quote(string)
  "\"#{string}\""
end

#random_colorObject



81
82
83
84
85
86
87
88
89
# File 'lib/dige/graphviz_diagram.rb', line 81

def random_color
  minimum = 10
  maximum = 210
  r = ((rand*maximum) + minimum).to_i
  g = ((rand*maximum) + minimum).to_i
  b = ((rand*maximum) + minimum).to_i
  
  "#" + r.to_s(16).rjust(2, '0') + g.to_s(16).rjust(2, '0') + b.to_s(16).rjust(2, '0')
end

#settingsObject



26
27
28
29
30
31
32
# File 'lib/dige/graphviz_diagram.rb', line 26

def settings
  [
    (!graph_settings.empty? ? "graph [" + graph_settings.collect { |k,v| "#{k}=#{quote(v)}" }.join(",") + "];" : nil),
    (!edge_settings.empty? ? "edge [" + edge_settings.collect { |k,v| "#{k}=#{quote(v)}" }.join(",") + "];" : nil),
    (!node_settings.empty? ? "node [" + node_settings.collect { |k,v| "#{k}=#{quote(v)}" }.join(",") + "];" : nil)
  ]
end