Class: TreeGraph::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_node, parent = nil) ⇒ Node

Returns a new instance of Node.



14
15
16
# File 'lib/tree_graph.rb', line 14

def initialize raw_node, parent=nil
  @raw_node, @parent, @is_last = raw_node, parent, false
end

Instance Attribute Details

#is_lastObject

Returns the value of attribute is_last.



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

def is_last
  @is_last
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#raw_nodeObject (readonly)

Returns the value of attribute raw_node.



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

def raw_node
  @raw_node
end

Instance Method Details

#ancestorsObject



38
39
40
41
# File 'lib/tree_graph.rb', line 38

def ancestors
  return [] unless parent
  parent.ancestors + [parent]
end

#branchObject



43
44
45
46
# File 'lib/tree_graph.rb', line 43

def branch
  return '' unless parent
  is_last ? '└─' : '├─'
end

#children_nodesObject



24
25
26
27
28
29
30
31
32
# File 'lib/tree_graph.rb', line 24

def children_nodes
  children = []
  raw_node.children_for_tree_graph.each do |c|
    children << Node.new(c, self)
  end
  return children if children.empty?
  children.last.is_last = true
  children
end

#indentObject



48
49
50
51
52
53
54
55
56
# File 'lib/tree_graph.rb', line 48

def indent
  ancestors.map do |a|
    unless a.parent
      ''
    else
      a.is_last ? '  ' : '│ '
    end
  end.join
end

#levelObject



34
35
36
# File 'lib/tree_graph.rb', line 34

def level
  [indent, branch, raw_node.label_for_tree_graph].join
end

#tree_graphObject



18
19
20
21
22
# File 'lib/tree_graph.rb', line 18

def tree_graph
  ([level] +
   children_nodes.map(&:tree_graph)
  ).join("\n")
end