File: dsl.rb

package info (click to toggle)
ruby-graphviz 1.0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,124 kB
  • ctags: 695
  • sloc: ruby: 7,656; xml: 26; makefile: 17
file content (67 lines) | stat: -rw-r--r-- 1,821 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'graphviz'

class GraphViz::DSL
   attr_accessor :graph

   # Create a new graph
   def initialize(name, options = {}, &block)
      @graph = GraphViz.new(name, options)
      instance_eval(&block) if block
   end

   def method_missing(sym, *args, &block) #:nodoc:
      return @graph.get_graph(sym.to_s) unless @graph.get_graph(sym.to_s).nil?
      return @graph.get_node(sym.to_s) unless @graph.get_node(sym.to_s).nil?
      if(@graph.respond_to?(sym, true))
         @graph.send(sym, *args)
      elsif(block)
         @graph.add_graph(GraphViz::DSL.new(sym, { :parent => @graph, :type => @graph.type }, &block).graph)
      else
         @graph.add_nodes(sym.to_s, *args)
      end
   end

   # Add a new node
   def n(name)
      return @graph.get_node(name) unless @graph.get_node(name.to_s).nil?
      @graph.add_nodes(name)
   end

   # Create edges
   def e(*args)
      e = nil
      last = args.shift
      while current = args.shift
         e = @graph.add_edges(last, current)
         last = current
      end
      return e
   end

   # Add a subgraph
   def subgraph(name, &block)
      @graph.add_graph(GraphViz::DSL.new(name, { :parent => @graph, :type => @graph.type }, &block).graph)
   end
   alias :cluster :subgraph

   # Generate output
   def output(options = {})
      @graph.output(options)
   end
end

# Create a new undirected graph
def graph(name, options = {}, &block)
   GraphViz::DSL.new(name, options.merge( { :type => "graph" } ), &block).graph
end

# Create a new directed graph
def digraph(name, options = {}, &block)
   GraphViz::DSL.new(name, options.merge( { :type => "digraph" } ), &block).graph
end

# Create a new strict directed graph
def strict(name, options = {}, &block)
   GraphViz::DSL.new(name, options.merge( { :type => "strict digraph" } ), &block).graph
end