Class: Tinydot::Digraph

Inherits:
Object
  • Object
show all
Defined in:
lib/tinydot/digraph.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, attrs = {}) ⇒ Digraph

Returns a new instance of Digraph.



5
6
7
8
9
10
# File 'lib/tinydot/digraph.rb', line 5

def initialize(name, attrs = {})
  @name = name
  @attrs = attrs
  @nodes = []
  @node_attrs = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tinydot/digraph.rb', line 22

def method_missing(name, *args)
  node = @nodes.select { |node| node.name == name }.first
  if node.nil?
    attrs = {}
    args.each do |arg|
      case arg
      when String then attrs[:label] = arg
      when Hash   then attrs.merge!(arg)
      end
    end

    node = Node.new(name, attrs)
    @nodes << node
  end
  node
end

Instance Method Details

#node(attrs = {}) ⇒ Object



12
13
14
# File 'lib/tinydot/digraph.rb', line 12

def node(attrs = {})
  @node_attrs = attrs
end

#to_dotObject



16
17
18
19
20
# File 'lib/tinydot/digraph.rb', line 16

def to_dot
  template_path = Tinydot.root_path.join("lib/tinydot/template.dot.erb")
  template = ERB.new(template_path.read, nil, "-")
  template.result(binding)
end