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

Version Path
katalyst-navigation-2.0.0 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.8.4 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.8.3 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.8.2 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.8.1 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.8.0 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.6.0 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.5.2 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.5.1 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.5.0 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.4.1 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.4.0 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.3.4 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.3.3 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.3.2 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.3.1 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.3.0 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.2.0 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.1.2 app/models/concerns/katalyst/navigation/has_tree.rb
katalyst-navigation-1.1.1 app/models/concerns/katalyst/navigation/has_tree.rb