Sha256: 9bd882d0d1f893a71c90e036050ba2283a54bfce97ef14332cd369fbeaf2201e

Contents?: true

Size: 947 Bytes

Versions: 14

Compression:

Stored size: 947 Bytes

Contents

module Lookbook
  class PreviewTreeBuilder < Service
    attr_reader :include_hidden

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

    def call
      root_node = TreeNode.new
      previews.each do |preview|
        current_node = root_node

        path_segments = preview.logical_path.split("/")
        path_segments.each.with_index(1) do |name, i|
          content = preview if preview.depth == i # entities are always on the leaf nodes

          current_node.add_child(name, content) unless current_node.has_child?(name)
          current_node = current_node.get_child(name)

          content&.visible_scenarios&.each do |scenario|
            current_node.add_child(scenario.name, scenario)
          end
        end
      end
      root_node
    end

    def previews
      include_hidden ? @previews : @previews.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/preview_tree_builder.rb
lookbook-2.3.3 lib/lookbook/services/entities/preview_tree_builder.rb
lookbook-2.3.2 lib/lookbook/services/entities/preview_tree_builder.rb
lookbook-2.3.0 lib/lookbook/services/entities/preview_tree_builder.rb
lookbook-2.2.2 lib/lookbook/services/entities/preview_tree_builder.rb
lookbook-2.2.1 lib/lookbook/services/entities/preview_tree_builder.rb
lookbook-2.2.0 lib/lookbook/services/entities/preview_tree_builder.rb
lookbook-2.1.1 lib/lookbook/services/entities/preview_tree_builder.rb
lookbook-2.1.0 lib/lookbook/services/entities/preview_tree_builder.rb
lookbook-2.0.5 lib/lookbook/services/entities/preview_tree_builder.rb
lookbook-2.0.4 lib/lookbook/services/entities/preview_tree_builder.rb
lookbook-2.0.3 lib/lookbook/services/entities/preview_tree_builder.rb
lookbook-2.0.2 lib/lookbook/services/entities/preview_tree_builder.rb
lookbook-2.0.1 lib/lookbook/services/entities/preview_tree_builder.rb