Sha256: a751221040923185541fe1ea3e44863868ad51f067952853f0f0d0db7f23fb48
Contents?: true
Size: 1.46 KB
Versions: 25
Compression:
Stored size: 1.46 KB
Contents
# frozen_string_literal: true module Katalyst module Navigation module HasTree # Constructs a tree from an ordered list of items with depth. # * items that have higher depth than their predecessor are nested as `children`. # * items with the same depth become siblings. def tree items.reduce(Builder.new, &:add).tree end class Builder attr_reader :current, :depth, :children, :tree def initialize @depth = 0 @children = @tree = [] end def add(node) if node.depth == depth node.parent = current children << node self elsif node.depth > depth push(children.last) add(node) else pop add(node) end end private attr_writer :current, :depth, :children # Add node to the top of builder stacks def push(node) self.depth += 1 self.current = node self.children = node.children = [] node end # Remove current from builder stack def pop previous = current self.depth -= 1 if depth.zero? self.current = nil self.children = tree else self.current = previous.parent self.children = current.children end previous end end end end end
Version data entries
25 entries across 25 versions & 1 rubygems