Class: Dhaka::Dot::Digraph

Inherits:
Object
  • Object
show all
Defined in:
lib/dhaka/dot/dot.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(node_attributes = {}) {|_self| ... } ⇒ Digraph

Returns a new instance of Digraph.

Yields:

  • (_self)

Yield Parameters:



4
5
6
7
8
9
# File 'lib/dhaka/dot/dot.rb', line 4

def initialize(node_attributes = {})
  @result = ["digraph x {"]
  @result << %(node #{dotify_hash(node_attributes)})
  yield(self)
  @result << '}'
end

Instance Method Details

#dotify_hash(hash) ⇒ Object



19
20
21
22
# File 'lib/dhaka/dot/dot.rb', line 19

def dotify_hash hash
  sorted_key_value_pairs = hash.collect {|key, value| [key.to_s, value.to_s]}.sort
  hash.empty? ? "" : '[' + sorted_key_value_pairs.collect {|key, value| "#{key}=#{value.to_s.inspect}"}.join(' ') + ']'
end

#edge(src, dest, attributes = {}) ⇒ Object



15
16
17
# File 'lib/dhaka/dot/dot.rb', line 15

def edge(src, dest, attributes = {})
  @result << "#{src.object_id} -> #{dest.object_id} #{dotify_hash(attributes)}"
end

#node(obj, attributes = {}) ⇒ Object



11
12
13
# File 'lib/dhaka/dot/dot.rb', line 11

def node(obj, attributes = {})
  @result << "#{obj.object_id} #{dotify_hash(attributes)}"
end

#to_dotObject



24
25
26
# File 'lib/dhaka/dot/dot.rb', line 24

def to_dot
  @result.join("\n")
end