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.



31
32
33
# File 'lib/tree_graph.rb', line 31

def is_last
  @is_last
end

#parentObject (readonly)

Returns the value of attribute parent.



32
33
34
# File 'lib/tree_graph.rb', line 32

def parent
  @parent
end

#raw_nodeObject (readonly)

Returns the value of attribute raw_node.



32
33
34
# File 'lib/tree_graph.rb', line 32

def raw_node
  @raw_node
end

Instance Method Details

#ancestorsObject



58
59
60
61
# File 'lib/tree_graph.rb', line 58

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

#childrenObject



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

def children
  raw_node.children_for_tree_graph
end

#children_nodesObject



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

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



63
64
65
66
67
68
69
70
71
# File 'lib/tree_graph.rb', line 63

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

#initialize(raw_node, parent = nil) ⇒ Object



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

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

#levelObject



50
51
52
# File 'lib/tree_graph.rb', line 50

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

#levelsObject



54
55
56
# File 'lib/tree_graph.rb', line 54

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