class DiagramGraph
Attributes
alphabetize[W]
diagram_type[W]
show_label[W]
Public Class Methods
new()
click to toggle source
# File lib/railroady/diagram_graph.rb, line 6 def initialize @diagram_type = '' @show_label = false @alphabetize = false @nodes = [] @edges = [] end
Public Instance Methods
add_edge(edge)
click to toggle source
# File lib/railroady/diagram_graph.rb, line 18 def add_edge(edge) @edges << edge end
add_node(node)
click to toggle source
# File lib/railroady/diagram_graph.rb, line 14 def add_node(node) @nodes << node end
to_dot()
click to toggle source
Generate DOT graph
# File lib/railroady/diagram_graph.rb, line 23 def to_dot dot_header + @nodes.map { |n| dot_node n[0], n[1], n[2], n[3] }.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/railroady/diagram_graph.rb, line 31 def to_xmi $stderr.print "Sorry. XMI output not yet implemented.\n\n" '' end
Private Instance Methods
dot_edge(type, from, to, name = '')
click to toggle source
Build a DOT graph edge
# File lib/railroady/diagram_graph.rb, line 99 def dot_edge(type, from, to, name = '') options = name != '' ? "label=\"#{name}\", " : '' edge_color = '"#%02X%02X%02X"' % [rand(255), rand(255), rand(255)] suffix = ", dir=both color=#{edge_color}" case type when 'one-one' options += "arrowtail=odot, arrowhead=dot#{suffix}" when 'one-many' options += "arrowtail=odot, arrowhead=crow#{suffix}" when 'many-many' options += "arrowtail=crow, arrowhead=crow#{suffix}" when 'belongs-to' # following http://guides.rubyonrails.org/association_basics.html#the-belongs-to-association options += "arrowtail=none, arrowhead=normal#{suffix}" when 'is-a' options += 'arrowhead="none", arrowtail="onormal"' when 'event' options += 'fontsize=10' end "\t#{quote(from)} -> #{quote(to)} [#{options}]\n" end
dot_header()
click to toggle source
Build DOT diagram header
# File lib/railroady/diagram_graph.rb, line 39 def dot_header result = "digraph #{@diagram_type.downcase}_diagram {\n" \ "\tgraph[overlap=false, splines=true, bgcolor=\"none\"]\n" result += dot_label if @show_label result end
dot_label()
click to toggle source
Build diagram label
# File lib/railroady/diagram_graph.rb, line 52 def dot_label "\t_diagram_info [shape=\"plaintext\", " \ "label=\"#{@diagram_type} diagram\\l" \ "Date: #{Time.now.strftime '%b %d %Y - %H:%M'}\\l" + (if defined?(ActiveRecord::Migrator) 'Migration version: ' \ "#{Rails.logger.silence { ActiveRecord::Migrator.current_version }}\\l" else '' end) + "Generated by #{APP_HUMAN_NAME} #{APP_VERSION}\\l" + 'http://railroady.prestonlee.com' \ "\\l\", fontsize=13]\n" end
dot_node(type, name, attributes = nil, custom_options = '')
click to toggle source
Build a DOT graph node
# File lib/railroady/diagram_graph.rb, line 67 def dot_node(type, name, attributes = nil, custom_options = '') case type when 'model' options = "shape=Mrecord, label=\"{#{name}|" options += attributes.sort_by { |s| @alphabetize ? s : nil }.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].sort_by { |s| @alphabetize ? s : nil }.join('\l') protected_methods = attributes[:protected].sort_by { |s| @alphabetize ? s : nil }.join('\l') private_methods = attributes[:private].sort_by { |s| @alphabetize ? s : nil }.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.gsub(/[^a-z0-9\-_]+/i, '_')} {\n\tlabel = #{quote(name)}\n\t#{attributes.join("\n ")}}" end options = [options, custom_options].compact.reject(&:empty?).join(', ') "\t#{quote(name)} [#{options}]\n" end
quote(name)
click to toggle source
Quotes a class name
# File lib/railroady/diagram_graph.rb, line 122 def quote(name) "\"#{name}\"" end