Class: Lookbook::Preview
Constant Summary
Constants included
from Utils
Utils::FRONTMATTER_REGEX, Utils::POSITION_PREFIX_REGEX
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Collection
#add, #as_tree, #clear, describe_as, #find, #find_by_id, #find_by_path, #find_first, #find_next, #find_parent, #find_previous, #get, #get_or_create, #name, #non_empty_items, #ordered_entities, #position, #visible_items
Methods inherited from Entity
#hidden?, #matchers, #path, #position
Constructor Details
#initialize(preview, code_object) ⇒ Preview
Returns a new instance of Preview.
8
9
10
11
12
|
# File 'lib/lookbook/preview.rb', line 8
def initialize(preview, code_object)
@preview = preview
@preview_inspector = SourceInspector.new(code_object)
super(preview_class_path(@preview.name))
end
|
Class Method Details
.all ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/lookbook/preview.rb', line 134
def all
if @previews.nil? && @preview_objects.present?
previews = @preview_objects.map do |code_object|
klass = code_object.path.constantize
new(klass, code_object) if klass.ancestors.include?(ViewComponent::Preview)
rescue => exception
Lookbook.logger.error Lookbook::Error.new(exception)
nil
end.compact
sorted_previews = previews.compact.sort_by { |preview| [preview.position, preview.label] }
@previews = PreviewCollection.new(sorted_previews)
else
PreviewCollection.new([])
end
@previews
end
|
.any? ⇒ Boolean
130
131
132
|
# File 'lib/lookbook/preview.rb', line 130
def any?
all.any?
end
|
.errors ⇒ Object
152
153
154
|
# File 'lib/lookbook/preview.rb', line 152
def errors
@errors ||= []
end
|
.exists?(path) ⇒ Boolean
126
127
128
|
# File 'lib/lookbook/preview.rb', line 126
def exists?(path)
!!find(path)
end
|
.find(path) ⇒ Object
122
123
124
|
# File 'lib/lookbook/preview.rb', line 122
def find(path)
all.find { |p| p.lookup_path == path }
end
|
.load!(preview_objects) ⇒ Object
156
157
158
159
|
# File 'lib/lookbook/preview.rb', line 156
def load!(preview_objects)
@preview_objects = preview_objects
@previews = nil
end
|
Instance Method Details
#collapsible? ⇒ Boolean
95
96
97
|
# File 'lib/lookbook/preview.rb', line 95
def collapsible?
true
end
|
#component ⇒ Object
99
100
101
|
# File 'lib/lookbook/preview.rb', line 99
def component
components.first
end
|
#components ⇒ Object
103
104
105
106
107
108
|
# File 'lib/lookbook/preview.rb', line 103
def components
component_classes = @preview_inspector&.components&.any? ? @preview_inspector&.components : [guess_component]
component_classes.map do |class_name|
Component.new(class_name.to_s)
end
end
|
#default_example ⇒ Object
60
61
62
|
# File 'lib/lookbook/preview.rb', line 60
def default_example
examples.first
end
|
#display_params ⇒ Object
91
92
93
|
# File 'lib/lookbook/preview.rb', line 91
def display_params
Lookbook.config.preview_display_params.deep_merge(@preview_inspector&.display_params)
end
|
#example(example_name) ⇒ Object
30
31
32
|
# File 'lib/lookbook/preview.rb', line 30
def example(example_name)
examples.find { |m| m.name == example_name.to_s }
end
|
#examples ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/lookbook/preview.rb', line 34
def examples
return @examples if @examples.present?
public_methods = @preview.public_instance_methods(false)
public_method_objects = @preview_inspector&.methods&.select { |m| public_methods.include?(m.name) }
examples = (public_method_objects || []).map { |m| PreviewExample.new(m.name.to_s, self, m) }
sorted = Lookbook.config.sort_examples ? examples.sort_by(&:label) : examples
@examples = []
if @preview_inspector&.groups&.any?
sorted.group_by { |m| m.group }.each do |name, examples|
if name.nil?
@examples += examples
else
name = label if name.strip == ""
@examples << PreviewGroup.new(name.underscore, self, examples)
end
end
else
@examples = sorted
end
@examples = @examples.compact
end
|
#full_path ⇒ Object
68
69
70
71
72
73
|
# File 'lib/lookbook/preview.rb', line 68
def full_path
base_path = Array(Lookbook.config.preview_paths).detect do |preview_path|
Dir["#{preview_path}/#{rel_path}"].first
end
Pathname.new(Dir["#{base_path}/#{rel_path}"].first)
end
|
#hierarchy_depth ⇒ Object
83
84
85
|
# File 'lib/lookbook/preview.rb', line 83
def hierarchy_depth
path.split("/").size
end
|
#id ⇒ Object
14
15
16
|
# File 'lib/lookbook/preview.rb', line 14
def id
@preview_inspector&.id || generate_id(lookup_path)
end
|
#items ⇒ Object
56
57
58
|
# File 'lib/lookbook/preview.rb', line 56
def items
examples.reject { |i| i.hidden? }
end
|
#label ⇒ Object
22
23
24
|
# File 'lib/lookbook/preview.rb', line 22
def label
@preview_inspector&.label&.presence || lookup_path.split("/").last.titleize
end
|
#layout ⇒ Object
87
88
89
|
# File 'lib/lookbook/preview.rb', line 87
def layout
@preview.instance_variable_get(:@layout)
end
|
#parent_collections_names ⇒ Object
79
80
81
|
# File 'lib/lookbook/preview.rb', line 79
def parent_collections_names
File.dirname(path).split("/")
end
|
#preview_class ⇒ Object
18
19
20
|
# File 'lib/lookbook/preview.rb', line 18
def preview_class
@preview.name
end
|
#rel_path ⇒ Object
64
65
66
|
# File 'lib/lookbook/preview.rb', line 64
def rel_path
"#{name.underscore}.rb"
end
|
#type ⇒ Object
26
27
28
|
# File 'lib/lookbook/preview.rb', line 26
def type
:preview
end
|
#url_path ⇒ Object
75
76
77
|
# File 'lib/lookbook/preview.rb', line 75
def url_path
lookbook_inspect_path lookup_path
end
|