Class: Lookbook::PreviewTreeBuilder

Inherits:
Service
  • Object
show all
Defined in:
lib/lookbook/services/entities/preview_tree_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Service

call

Constructor Details

#initialize(previews, include_hidden: false) ⇒ PreviewTreeBuilder

Returns a new instance of PreviewTreeBuilder.



5
6
7
8
# File 'lib/lookbook/services/entities/preview_tree_builder.rb', line 5

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

Instance Attribute Details

#include_hiddenObject (readonly)



3
4
5
# File 'lib/lookbook/services/entities/preview_tree_builder.rb', line 3

def include_hidden
  @include_hidden
end

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lookbook/services/entities/preview_tree_builder.rb', line 10

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

#previewsObject



30
31
32
# File 'lib/lookbook/services/entities/preview_tree_builder.rb', line 30

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