Class: Lookbook::PreviewCollection

Inherits:
EntityCollection show all
Includes:
HierarchicalCollection
Defined in:
lib/lookbook/entities/collections/preview_collection.rb

Constant Summary collapse

TREE_BUILDER =
PreviewTreeBuilder

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EntityCollection

#add, #clear_all, #each, #find_by_id, #find_by_path, #flat_map, #initialize, #next, #previous

Constructor Details

This class inherits a constructor from Lookbook::EntityCollection

Class Method Details

.preview_class?(klass) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 62

def preview_class?(klass)
  if (defined?(ViewComponent) && klass.ancestors.include?(ViewComponent::Preview)) || klass.ancestors.include?(Lookbook::Preview)
    !klass.respond_to?(:abstract_class) || klass.abstract_class != true
  end
end

.preview_from_code_object(code_object) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 51

def preview_from_code_object(code_object)
  klass = code_object.path.constantize
  if preview_class?(klass)
    preview = PreviewEntity.new(code_object)
    preview if preview.scenarios.any?
  end
rescue => exception
  Lookbook.logger.error exception.to_s
  nil
end

Instance Method Details

#entitiesObject



46
47
48
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 46

def entities
  @_cache[:entities] ||= collect_ordered_entities(to_tree(include_hidden: true)).grep(Lookbook::PreviewEntity)
end

#find_by_preview_class(klass) ⇒ Object



11
12
13
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 11

def find_by_preview_class(klass)
  find { |preview| preview.preview_class.name == klass.to_s }
end

#find_scenario_by_path(lookup_path) ⇒ Object



7
8
9
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 7

def find_scenario_by_path(lookup_path)
  scenarios.find_by_path(lookup_path)
end

#load(code_objects, changes = nil) ⇒ Object



15
16
17
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 15

def load(code_objects, changes = nil)
  changes.present? ? reload_changed(code_objects, changes) : reload_all(code_objects)
end

#reload_all(code_objects) ⇒ Object



19
20
21
22
23
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 19

def reload_all(code_objects)
  clear_all
  previews = code_objects.map { |obj| PreviewCollection.preview_from_code_object(obj) }.compact
  add(previews)
end

#reload_changed(code_objects, changes) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 25

def reload_changed(code_objects, changes)
  modified = Array(changes[:modified])
  removed = Array(changes[:removed]) + modified
  added = Array(changes[:added]) + modified

  remove_by_file_path(removed)

  previews = added.map do |path|
    code_object = code_objects.find { |obj| obj if obj&.file.to_s == path.to_s }
    PreviewCollection.preview_from_code_object(code_object) if code_object
  end.compact

  add(previews)
end

#remove_by_file_path(paths) ⇒ Object



40
41
42
43
44
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 40

def remove_by_file_path(paths)
  paths = Array(paths).map(&:to_s)
  @entities.reject! { |preview| preview.file_path.to_s.in?(paths) }
  clear_cache
end