Class: GraphViz::MindMap::Node
- Inherits:
-
Object
- Object
- GraphViz::MindMap::Node
- Defined in:
- lib/graph_viz/mind_map/node.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#nodes ⇒ Object
Returns the value of attribute nodes.
-
#options ⇒ Object
Returns the value of attribute options.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#pass_on_options ⇒ Object
Returns the value of attribute pass_on_options.
-
#used_identifiers ⇒ Object
Returns the value of attribute used_identifiers.
Instance Method Summary collapse
-
#at(dot_path) ⇒ Object
tree walking things.
- #build_dot_at_index(o, idx) ⇒ Object
- #build_dot_from_root ⇒ Object
- #dot_identifier ⇒ Object
- #dot_options ⇒ Object
- #inherit! ⇒ Object
-
#initialize(*args) ⇒ Node
constructor
A new instance of Node.
- #make_dot_identifier ⇒ Object
- #no_inherit! ⇒ Object
- #node(*args, &block) ⇒ Object
-
#node_def ⇒ Object
dot generation things.
- #root ⇒ Object
-
#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.
Constructor Details
#initialize(*args) ⇒ Node
Returns a new instance of Node.
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
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/graph_viz/mind_map/node.rb', line 5 def name @name end |
#nodes ⇒ Object
Returns the value of attribute nodes.
8 9 10 |
# File 'lib/graph_viz/mind_map/node.rb', line 8 def nodes @nodes end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/graph_viz/mind_map/node.rb', line 6 def @options end |
#parent ⇒ Object
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_options ⇒ Object
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 end |
#used_identifiers ⇒ Object
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_root ⇒ Object
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 = if opts.length > 0 o << " graph #{}" end nodes.each do |node| node.to_dot(o, 1) end o << "}" o.join("\n") end |
#dot_identifier ⇒ Object
65 66 67 |
# File 'lib/graph_viz/mind_map/node.rb', line 65 def dot_identifier @dot_identifier ||= make_dot_identifier end |
#dot_options ⇒ Object
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 o=[] unless parent.nil? o << "label=\"#{name}\"" end .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_identifier ⇒ Object
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. = .dup.merge(n.) n. = true end end if block_given? @nodes.last.instance_eval &block end end |
#node_def ⇒ Object
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 = o = [dot_identifier] if opts.length > 0 o << ' ' o << opts end o.join end |
#root ⇒ Object
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 |