Class: GraphViz::MindMap::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/graph_viz/mind_map/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Node

Returns a new instance of Node.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
# File 'lib/graph_viz/mind_map/node.rb', line 12

def initialize *args
  @options = args.last.is_a?(Hash) ? args.pop : {}
  @name = args.first
  @nodes = []
  @used_identifiers = []
  @pass_on_options = false
  raise ArgumentError.new("You need to name this mind map") unless name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/graph_viz/mind_map/node.rb', line 5

def name
  @name
end

#nodesObject

Returns the value of attribute nodes.



8
9
10
# File 'lib/graph_viz/mind_map/node.rb', line 8

def nodes
  @nodes
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/graph_viz/mind_map/node.rb', line 6

def options
  @options
end

#parentObject

Returns the value of attribute parent.



7
8
9
# File 'lib/graph_viz/mind_map/node.rb', line 7

def parent
  @parent
end

#pass_on_optionsObject

Returns the value of attribute pass_on_options.



10
11
12
# File 'lib/graph_viz/mind_map/node.rb', line 10

def pass_on_options
  @pass_on_options
end

#used_identifiersObject

Returns the value of attribute used_identifiers.



9
10
11
# File 'lib/graph_viz/mind_map/node.rb', line 9

def used_identifiers
  @used_identifiers
end

Instance Method Details

#at(dot_path) ⇒ Object

tree walking things



39
40
41
42
43
44
45
46
47
# File 'lib/graph_viz/mind_map/node.rb', line 39

def at(dot_path)
  path = dot_path.split(/\./)
  if path.any?
    index = path.shift.to_i
    nodes[index].at(path.join('.'))
  else
    self
  end
end

#build_dot_at_index(o, idx) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/graph_viz/mind_map/node.rb', line 116

def build_dot_at_index(o, idx)
  leader = " "*(idx * 2)
  o << "#{leader}#{node_def}"
  if parent && idx > 1
    o << "#{leader}#{parent.dot_identifier} -- #{dot_identifier}"
  end
  if nodes.any?
    o << "#{leader}subgraph sg_#{dot_identifier} {"
    nodes.each do |node|
      node.to_dot(o, idx + 1)
    end
    o << "#{leader}}"
  end
end

#build_dot_from_rootObject



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/graph_viz/mind_map/node.rb', line 102

def build_dot_from_root
  o = []
  o << "graph #{dot_identifier} {"
  opts = dot_options
  if opts.length > 0
    o << "  graph #{dot_options}"
  end
  nodes.each do |node|
    node.to_dot(o, 1)
  end
  o << "}"
  o.join("\n")
end

#dot_identifierObject



65
66
67
# File 'lib/graph_viz/mind_map/node.rb', line 65

def dot_identifier
  @dot_identifier ||= make_dot_identifier
end

#dot_optionsObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/graph_viz/mind_map/node.rb', line 78

def dot_options
  o=[]
  unless parent.nil?
    o << "label=\"#{name}\""
  end
  options.each_pair do |k,v|
    o << "#{k}=\"#{v}\""
  end
  if o.any?
    "[#{o.join(' ')}]"
  else
    ''
  end
end

#inherit!Object



21
# File 'lib/graph_viz/mind_map/node.rb', line 21

def inherit!; @pass_on_options = true end

#make_dot_identifierObject



69
70
71
72
73
74
75
76
# File 'lib/graph_viz/mind_map/node.rb', line 69

def make_dot_identifier
  id = name.downcase.gsub(/[^ a-z]/,'').sub(/ +$/,'').gsub(/ +/,'_')
  if root.used_identifiers.include? id
    id = "#{parent.dot_identifier}_#{id}"
  end
  root.used_identifiers << id
  id
end

#no_inherit!Object



22
# File 'lib/graph_viz/mind_map/node.rb', line 22

def no_inherit!; @pass_on_options = false end

#node(*args, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/graph_viz/mind_map/node.rb', line 24

def node(*args, &block)
  @nodes << Node.new(*args).tap do |n| 
    n.parent = self
    if @pass_on_options
      n.options = options.dup.merge(n.options)
      n.pass_on_options = true
    end
  end
  if block_given?
    @nodes.last.instance_eval &block
  end
end

#node_defObject

dot generation things



55
56
57
58
59
60
61
62
63
# File 'lib/graph_viz/mind_map/node.rb', line 55

def node_def
  opts = dot_options
  o = [dot_identifier]
  if opts.length > 0
    o << ' '
    o << opts
  end
  o.join
end

#rootObject



49
50
51
# File 'lib/graph_viz/mind_map/node.rb', line 49

def root
  parent ? parent.root : self
end

#to_dot(output = nil, indent = 0) ⇒ Object

assumes the node you call #to_dot on is the root of the graph and should be treated like a graph



94
95
96
97
98
99
100
# File 'lib/graph_viz/mind_map/node.rb', line 94

def to_dot(output=nil, indent=0)
  if indent == 0
    build_dot_from_root
  else
    build_dot_at_index(output,indent)
  end
end