Sha256: 27409faff00098c73ccfc40a9a7566a200250c8935117a88e497f3614d822b86

Contents?: true

Size: 844 Bytes

Versions: 2

Compression:

Stored size: 844 Bytes

Contents

module Treedent
  class Tree < Struct.new(:indented_lines)
    include Enumerable

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

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
treedent-1.1.0 lib/treedent/tree.rb
treedent-1.0.0 lib/treedent/tree.rb