Class: Lookbook::PreviewCollection

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

Instance Attribute Summary

Attributes inherited from EntityCollection

#entities

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EntityCollection

#add, #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)


29
30
31
32
33
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 29

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

.preview_from_code_object(code_object) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 21

def self.preview_from_code_object(code_object)
  klass = code_object.path.constantize
  Preview.new(code_object) if preview_class?(klass)
rescue => exception
  Lookbook.logger.error exception.to_s
  nil
end

Instance Method Details

#find_by_preview_class(klass) ⇒ Object



9
10
11
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 9

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

#find_example_by_path(lookup_path) ⇒ Object



5
6
7
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 5

def find_example_by_path(lookup_path)
  examples.find_by_path(lookup_path)
end

#load(code_objects) ⇒ Object



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

def load(code_objects)
  @entities = []
  clear_cache

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