class DiagramGraph

RailRoad diagram structure

Public Class Methods

new() click to toggle source
# File lib/railroad/diagram_graph.rb, line 11
def initialize
  @diagram_type = ''
  @show_label = false
  @nodes = []
  @edges = []
end

Public Instance Methods

add_edge(edge) click to toggle source
# File lib/railroad/diagram_graph.rb, line 22
def add_edge(edge)
  @edges << edge
end
add_node(node) click to toggle source
# File lib/railroad/diagram_graph.rb, line 18
def add_node(node)
  @nodes << node
end
diagram_type=(type) click to toggle source
# File lib/railroad/diagram_graph.rb, line 26
def diagram_type= (type)
  @diagram_type = type
end
show_label=(value) click to toggle source
# File lib/railroad/diagram_graph.rb, line 30
def show_label= (value)
  @show_label = value
end
to_dot() click to toggle source

Generate DOT graph

# File lib/railroad/diagram_graph.rb, line 36
def to_dot
  return dot_header +
         @nodes.map{|n| dot_node n[0], n[1], n[2]}.join +
         @edges.map{|e| dot_edge e[0], e[1], e[2], e[3]}.join +
         dot_footer
end
to_xmi() click to toggle source

Generate XMI diagram (not yet implemented)

# File lib/railroad/diagram_graph.rb, line 44
def to_xmi
   STDERR.print "Sorry. XMI output not yet implemented.\n\n"
   return ""
end

Private Instance Methods

dot_edge(type, from, to, name = '') click to toggle source

Build a DOT graph edge

# File lib/railroad/diagram_graph.rb, line 108
def dot_edge(type, from, to, name = '')
  options =  name != '' ? "label=\"#{name}\", " : ''
  case type
    when 'one-one'
         #options += 'taillabel="1"'
         options += 'arrowtail=odot, arrowhead=dot, dir=both'
    when 'one-many'
         #options += 'taillabel="n"'
         options += 'arrowtail=crow, arrowhead=dot, dir=both'                    
    when 'many-many'
         #options += 'taillabel="n", headlabel="n", arrowtail="normal"'
         options += 'arrowtail=crow, arrowhead=crow, dir=both'
    when 'is-a'
         options += 'arrowhead="none", arrowtail="onormal"'
    when 'event'
         options += "fontsize=10"
  end
  return "\t#{quote(from)} -> #{quote(to)} [#{options}]\n"
end
dot_header() click to toggle source

Build DOT diagram header

# File lib/railroad/diagram_graph.rb, line 52
def dot_header
  result = "digraph #{@diagram_type.downcase}_diagram {\n" +
           "\tgraph[overlap=false, splines=true]\n"
  result += dot_label if @show_label
  return result
end
dot_label() click to toggle source

Build diagram label

# File lib/railroad/diagram_graph.rb, line 65
def dot_label
  return "\t_diagram_info [shape=\"plaintext\", " +
         "label=\"#{@diagram_type} diagram\\l" +
         "Date: #{Time.now.strftime "%b %d %Y - %H:%M"}\\l" + 
         "Migration version: " +
         "#{ActiveRecord::Migrator.current_version}\\l" +
         "Generated by #{APP_HUMAN_NAME} #{APP_VERSION.join('.')}"+
         "\\l\", fontsize=14]\n"
end
dot_node(type, name, attributes=nil) click to toggle source

Build a DOT graph node

# File lib/railroad/diagram_graph.rb, line 76
def dot_node(type, name, attributes=nil)
  case type
    when 'model'
         options = 'shape=Mrecord, label="{' + name + '|'
         options += attributes.join('\l')
         options += '\l}"'
    when 'model-brief'
         options = ''
    when 'class'
         options = 'shape=record, label="{' + name + '|}"' 
    when 'class-brief'
         options = 'shape=box' 
    when 'controller'
         options = 'shape=Mrecord, label="{' + name + '|'
         public_methods    = attributes[:public].join('\l')
         protected_methods = attributes[:protected].join('\l')
         private_methods   = attributes[:private].join('\l')
         options += public_methods + '\l|' + protected_methods + '\l|' + 
                    private_methods + '\l'
         options += '}"'
    when 'controller-brief'
         options = '' 
    when 'module'
         options = 'shape=box, style=dotted, label="' + name + '"'
    when 'aasm'
         # Return subgraph format
         return "subgraph cluster_#{name.downcase} {\n\tlabel = #{quote(name)}\n\t#{attributes.join("\n  ")}}"
  end # case
  return "\t#{quote(name)} [#{options}]\n"
end
quote(name) click to toggle source

Quotes a class name

# File lib/railroad/diagram_graph.rb, line 129
def quote(name)
  '"' + name.to_s + '"'
end