Module: TreeGraph::Node

Included in:
BottomUp, TopDown
Defined in:
lib/tree_graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#is_lastObject

Returns the value of attribute is_last.



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

def is_last
  @is_last
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#raw_nodeObject (readonly)

Returns the value of attribute raw_node.



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

def raw_node
  @raw_node
end

Instance Method Details

#ancestorsObject



46
47
48
49
# File 'lib/tree_graph.rb', line 46

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

#childrenObject



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

def children
  raw_node.children_for_tree_graph
end

#children_nodesObject



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

def children_nodes
  children.map do |c|
    self.class.new(c, self)
  end.tap do |nodes|
    nodes.last.is_last = true unless nodes.empty?
  end
end

#indentObject



51
52
53
54
55
56
57
58
59
# File 'lib/tree_graph.rb', line 51

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

#initialize(raw_node, parent = nil) ⇒ Object



22
23
24
# File 'lib/tree_graph.rb', line 22

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

#levelObject



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

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

#levelsObject



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

def levels
  [level] + children_nodes.map(&:tree_graph)
end