Class: Treedent::Tree
- Inherits:
-
Struct
- Object
- Struct
- Treedent::Tree
- Includes:
- Enumerable
- Defined in:
- lib/treedent/tree.rb
Instance Attribute Summary collapse
-
#indented_lines ⇒ Object
Returns the value of attribute indented_lines.
Instance Method Summary collapse
Instance Attribute Details
#indented_lines ⇒ Object
Returns the value of attribute indented_lines
2 3 4 |
# File 'lib/treedent/tree.rb', line 2 def indented_lines @indented_lines end |
Instance Method Details
#each ⇒ Object
5 6 7 8 |
# File 'lib/treedent/tree.rb', line 5 def each node = root yield node while node = node.successor end |
#root ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/treedent/tree.rb', line 10 def root root_node = Node.new parent_node = root_node prev_node = root_node indented_lines.each do |line| unless prev_node.root? if line.indentation != prev_node.value.indentation parent_node = prev_node if line.indentation < prev_node.value.indentation parent_node = prev_node.ancestors.find do |parent| parent.root? || parent.value.indentation < line.indentation end end end end node = Node.new(line, parent_node) prev_node.successor = node prev_node = node end root_node end |