Sha256: 2cdf85d3e916561538c3002c11b4cdfeb986d49565bbb7982095e93828906310

Contents?: true

Size: 1.3 KB

Versions: 14

Compression:

Stored size: 1.3 KB

Contents

module Lookbook
  class EntityTreeBuilder < Service
    attr_reader :include_hidden

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

    def call
      root_node = TreeNode.new
      entities.each do |entity|
        current_node = root_node
        path_segments = parse_segments(entity.logical_path)
        path_segments.each.with_index(1) do |segment, i|
          name, priority_prefix = segment
          content = entity if entity.depth == i # entities 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)

          if content && content.type == :preview
            content.visible_scenarios.each do |scenario|
              current_node.add_child(scenario.name, scenario)
            end
          end
        end
      end
      root_node
    end

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

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

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
lookbook-2.0.0 lib/lookbook/services/entities/entity_tree_builder.rb
lookbook-2.0.0.rc.3 lib/lookbook/services/entities/entity_tree_builder.rb
lookbook-2.0.0.rc.2 lib/lookbook/services/entities/entity_tree_builder.rb
lookbook-2.0.0.rc.1 lib/lookbook/services/entities/entity_tree_builder.rb
lookbook-2.0.0.beta.9 lib/lookbook/services/entities/entity_tree_builder.rb
lookbook-2.0.0.beta.8 lib/lookbook/services/entities/entity_tree_builder.rb
lookbook-2.0.0.beta.7 lib/lookbook/services/entities/entity_tree_builder.rb
lookbook-2.0.0.beta.6 lib/lookbook/services/entities/entity_tree_builder.rb
lookbook-2.0.0.beta.5 lib/lookbook/services/entities/entity_tree_builder.rb
lookbook-2.0.0.beta.4 lib/lookbook/services/entities/entity_tree_builder.rb
lookbook-2.0.0.beta.3 lib/lookbook/services/entities/entity_tree_builder.rb
lookbook-2.0.0.beta.2 lib/lookbook/services/entities/entity_tree_builder.rb
lookbook-2.0.0.beta.1 lib/lookbook/services/entities/entity_tree_builder.rb
lookbook-2.0.0.beta.0 lib/lookbook/services/entities/entity_tree_builder.rb