Class: TreeGraph::Node
- Inherits:
-
Object
- Object
- TreeGraph::Node
- Defined in:
- lib/tree_graph.rb
Instance Attribute Summary collapse
-
#is_last ⇒ Object
Returns the value of attribute is_last.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#raw_node ⇒ Object
readonly
Returns the value of attribute raw_node.
Instance Method Summary collapse
- #ancestors ⇒ Object
- #branch ⇒ Object
- #children_nodes ⇒ Object
- #indent ⇒ Object
-
#initialize(raw_node, parent = nil) ⇒ Node
constructor
A new instance of Node.
- #level ⇒ Object
- #tree_graph ⇒ Object
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_last ⇒ Object
Returns the value of attribute is_last.
11 12 13 |
# File 'lib/tree_graph.rb', line 11 def is_last @is_last end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
12 13 14 |
# File 'lib/tree_graph.rb', line 12 def parent @parent end |
#raw_node ⇒ Object (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
#ancestors ⇒ Object
38 39 40 41 |
# File 'lib/tree_graph.rb', line 38 def ancestors return [] unless parent parent.ancestors + [parent] end |
#branch ⇒ Object
43 44 45 46 |
# File 'lib/tree_graph.rb', line 43 def branch return '' unless parent is_last ? '└─' : '├─' end |
#children_nodes ⇒ Object
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 |
#indent ⇒ Object
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 |
#level ⇒ Object
34 35 36 |
# File 'lib/tree_graph.rb', line 34 def level [indent, branch, raw_node.label_for_tree_graph].join end |
#tree_graph ⇒ Object
18 19 20 21 22 |
# File 'lib/tree_graph.rb', line 18 def tree_graph ([level] + children_nodes.map(&:tree_graph) ).join("\n") end |