lib/graph.rb in aws_security_viz-0.1.2.pre.alpha.pre.58 vs lib/graph.rb in aws_security_viz-0.1.2.pre.alpha.pre.59
- old
+ new
@@ -1,39 +1,46 @@
-require 'graphviz'
-require 'logger'
-
class Graph
- def initialize
- @g = GraphViz::new('G', :type => 'strict digraph') { |g|
- g[:overlap] = :false
- g[:splines] = :true
- g[:sep] = 1
- g[:concentrate] = :true
- }
+ attr_reader :ops
+
+ def initialize(config)
+ @config = config
+ @ops = []
+ @nodes = Set.new
end
def add_node(name)
log("node: #{name}")
- @g.add_node(name) if name
+ uniquely_add(@ops, :node, name) {
+ [:node, name]
+ }
end
+ #TODO : Dedup edge labels ?
+ #TODO : Remove env dependencies
- def get_node(name, &block)
- @g.get_node(name, &block)
- end
-
def add_edge(from, to, opts)
log("edge: #{from} -> #{to}")
- @g.add_edge(from, to, opts)
+ add_node(from)
+ add_node(to)
+ uniquely_add(@ops, :edge, from, to) {
+ [:edge, from, to, opts]
+ }
end
- def each_edge(&block)
- @g.each_edge(&block)
+ def output(renderer)
+ @ops.each { |op, *args|
+ renderer.add_node(*args) if op==:node
+ renderer.add_edge(*args) if op==:edge
+ }
+ renderer.output
end
- def output(opts)
- log("output: #{opts}")
- @g.output(opts)
+ def uniquely_add(target, type, *opts, &block)
+ return if opts.compact.empty?
+ return if @nodes.include?([type, opts])
+ @nodes.add([type, opts])
+ target << yield
end
+
def log(msg)
puts msg if ENV["DEBUG"]
end
end