Class: Treedent::Tree

Inherits:
Struct
  • Object
show all
Includes:
Enumerable
Defined in:
lib/treedent/tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#indented_linesObject

Returns the value of attribute indented_lines

Returns:

  • (Object)

    the current value of indented_lines



2
3
4
# File 'lib/treedent/tree.rb', line 2

def indented_lines
  @indented_lines
end

Instance Method Details

#eachObject



5
6
7
8
# File 'lib/treedent/tree.rb', line 5

def each
  node = root
  yield node while node = node.successor
end

#rootObject



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