Class: Lookbook::PreviewCollection
Instance Attribute Summary
#entities
Class Method Summary
collapse
Instance Method Summary
collapse
#add, #clear_all, #each, #find_by_id, #find_by_path, #flat_map, #initialize, #next, #previous
Class Method Details
.preview_class?(klass) ⇒ Boolean
56
57
58
59
60
|
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 56
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
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 45
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
#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_scenario_by_path(lookup_path) ⇒ Object
5
6
7
|
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 5
def find_scenario_by_path(lookup_path)
scenarios.find_by_path(lookup_path)
end
|
#load(code_objects, changes = nil) ⇒ Object
13
14
15
|
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 13
def load(code_objects, changes = nil)
changes.present? ? reload_changed(code_objects, changes) : reload_all(code_objects)
end
|
#reload_all(code_objects) ⇒ Object
17
18
19
20
21
|
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 17
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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 23
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
38
39
40
41
42
|
# File 'lib/lookbook/entities/collections/preview_collection.rb', line 38
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
|