Class: CooCoo::Dot::Writer

Inherits:
Object show all
Defined in:
lib/coo-coo/dot.rb

Instance Method Summary collapse

Constructor Details

#initializeWriter

Returns a new instance of Writer.



71
72
# File 'lib/coo-coo/dot.rb', line 71

def initialize
end

Instance Method Details

#indent(size) ⇒ Object



92
93
94
# File 'lib/coo-coo/dot.rb', line 92

def indent(size)
  "  " * size
end

#start_block(kind, depth) ⇒ Object



102
103
104
105
106
107
# File 'lib/coo-coo/dot.rb', line 102

def start_block(kind, depth)
  [ indent(depth) + "#{kind} {",
    *yield(depth + 1),
    indent(depth) + "}"
  ]
end

#write(graph, io) ⇒ Object



74
75
76
77
# File 'lib/coo-coo/dot.rb', line 74

def write(graph, io)
  io.write(write_graph(graph).join("\n"))
  self
end

#write_edges(edges, depth) ⇒ Object



115
116
117
118
119
120
# File 'lib/coo-coo/dot.rb', line 115

def write_edges(edges, depth)
  edges.collect do |edge|
    nodes = edge.nodes.join(" -> ")
    indent(depth) + "#{nodes}[#{write_options(edge.options)}];"
  end
end

#write_graph(g, depth = 0) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/coo-coo/dot.rb', line 79

def write_graph(g, depth = 0)
  start_block("#{g.kind}", depth) do |d|
    lines = []
    lines += write_graph_options(g, d)
    g.blocks.each do |kid|
      lines += write_graph(kid, d)
    end
    lines += write_nodes(g.nodes, d)
    lines += write_edges(g.edges, d)
    lines
  end
end

#write_graph_options(graph, depth) ⇒ Object



96
97
98
99
100
# File 'lib/coo-coo/dot.rb', line 96

def write_graph_options(graph, depth)
  graph.options.collect do |key, value|
    indent(depth) + "#{key}=\"#{value}\";"
  end
end

#write_nodes(nodes, depth) ⇒ Object



109
110
111
112
113
# File 'lib/coo-coo/dot.rb', line 109

def write_nodes(nodes, depth)
  nodes.collect do |node|
    indent(depth) + "#{node.name}[#{write_options(node.options)}];"
  end
end

#write_options(options) ⇒ Object



122
123
124
125
126
# File 'lib/coo-coo/dot.rb', line 122

def write_options(options)
  options.collect do |key, value|
    "#{key}=\"#{value}\""
  end.join(",")
end