Sha256: b15c1d2576078258f9f120f622425a696d477ed9a45142768b9ee0c59c08592b

Contents?: true

Size: 1.08 KB

Versions: 14

Compression:

Stored size: 1.08 KB

Contents

module Lookbook
  class PageTreeBuilder < Service
    attr_reader :include_hidden

    def initialize(pages, include_hidden: false)
      @pages = pages.to_a
      @include_hidden = include_hidden
    end

    def call
      root_node = TreeNode.new
      pages.each do |page|
        current_node = root_node

        path_segments = parse_segments(page.relative_file_path)
        path_segments.each.with_index(1) do |segment, i|
          name, priority_prefix = segment
          content = page if page.depth == i # pages are always on the leaf nodes

          current_node.add_child(name, content, priority: priority_prefix) unless current_node.has_child?(name)
          current_node = current_node.get_child(name)
        end
      end
      root_node
    end

    def parse_segments(path)
      path.to_s.split("/").map do |segment|
        unless segment.start_with?(".")
          priority, name = PriorityPrefixParser.call(segment)
          [name, priority || 10000]
        end
      end.compact
    end

    def pages
      include_hidden ? @pages : @pages.select(&:visible?)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
lookbook-2.3.4 lib/lookbook/services/entities/page_tree_builder.rb
lookbook-2.3.3 lib/lookbook/services/entities/page_tree_builder.rb
lookbook-2.3.2 lib/lookbook/services/entities/page_tree_builder.rb
lookbook-2.3.0 lib/lookbook/services/entities/page_tree_builder.rb
lookbook-2.2.2 lib/lookbook/services/entities/page_tree_builder.rb
lookbook-2.2.1 lib/lookbook/services/entities/page_tree_builder.rb
lookbook-2.2.0 lib/lookbook/services/entities/page_tree_builder.rb
lookbook-2.1.1 lib/lookbook/services/entities/page_tree_builder.rb
lookbook-2.1.0 lib/lookbook/services/entities/page_tree_builder.rb
lookbook-2.0.5 lib/lookbook/services/entities/page_tree_builder.rb
lookbook-2.0.4 lib/lookbook/services/entities/page_tree_builder.rb
lookbook-2.0.3 lib/lookbook/services/entities/page_tree_builder.rb
lookbook-2.0.2 lib/lookbook/services/entities/page_tree_builder.rb
lookbook-2.0.1 lib/lookbook/services/entities/page_tree_builder.rb