Class: TodoNext::Tree::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/todo_next/tree/factory.rb

Class Method Summary collapse

Class Method Details

.build(lines) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/todo_next/tree/factory.rb', line 9

def self.build(lines)
  mark_the_leaves_and_the_branches(lines)

  tree    = Tree.new
  parents = ParentsList.new

  lines.each do |line|
    curr_line_col = line.col_offset
    parent        = parents.get_for_item_at_column(curr_line_col) || tree

    new_node = if     line.example? then Tree::EX.new(line.text, parent)
               elsif  line.branch?  then Tree::OL.new(line.text, parent)
               else                      Tree::LI.new(line.text, parent)
               end
    parent.children << new_node

    if line.branch?
      parents.register_parent(new_node, :for_col => curr_line_col)
    end
  end
  tree
end