Class: Lookbook::Preview

Inherits:
Collection show all
Includes:
Utils
Defined in:
lib/lookbook/preview.rb

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
13
14
15
16
17
18
19
# File 'lib/lookbook/preview.rb', line 8

def initialize(preview, code_object)
  @preview = preview
  @preview_inspector = SourceInspector.new(code_object, eval_scope: preview_class.new)
  preview_path = preview_class_path(name)

  if @preview_inspector.logical_path
    basename = preview_path.split("/").last
    preview_path = "#{@preview_inspector.logical_path}/#{basename}"
  end

  super(preview_path)
end

Class Method Details

.allObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/lookbook/preview.rb', line 149

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)
    @previews
  elsif !@preview_objects.present?
    PreviewCollection.new([])
  else
    @previews
  end
end

.any?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/lookbook/preview.rb', line 145

def any?
  all.any?
end

.errorsObject



169
170
171
# File 'lib/lookbook/preview.rb', line 169

def errors
  @errors ||= []
end

.exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/lookbook/preview.rb', line 141

def exists?(path)
  !!find(path)
end

.find(path) ⇒ Object



137
138
139
# File 'lib/lookbook/preview.rb', line 137

def find(path)
  all.find { |p| p.lookup_path == path }
end

.load!(preview_objects) ⇒ Object



173
174
175
176
# File 'lib/lookbook/preview.rb', line 173

def load!(preview_objects)
  @preview_objects = preview_objects
  @previews = nil
end

Instance Method Details

#collapsible?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/lookbook/preview.rb', line 106

def collapsible?
  true
end

#componentObject



110
111
112
# File 'lib/lookbook/preview.rb', line 110

def component
  components.first
end

#componentsObject



114
115
116
117
118
119
# File 'lib/lookbook/preview.rb', line 114

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_exampleObject



71
72
73
# File 'lib/lookbook/preview.rb', line 71

def default_example
  examples.first
end

#display_optionsObject



102
103
104
# File 'lib/lookbook/preview.rb', line 102

def display_options
  Lookbook.config.preview_display_options.deep_merge(@preview_inspector&.display_options)
end

#example(example_name) ⇒ Object



41
42
43
# File 'lib/lookbook/preview.rb', line 41

def example(example_name)
  examples.find { |m| m.name == example_name.to_s }
end

#examplesObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lookbook/preview.rb', line 45

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_pathObject



79
80
81
82
83
84
# File 'lib/lookbook/preview.rb', line 79

def full_path
  base_path = preview_paths.detect do |preview_path|
    Dir["#{preview_path}/#{rel_path}"].first
  end
  Pathname.new(Dir["#{base_path}/#{rel_path}"].first)
end

#hierarchy_depthObject



94
95
96
# File 'lib/lookbook/preview.rb', line 94

def hierarchy_depth
  path.split("/").size
end

#idObject



21
22
23
# File 'lib/lookbook/preview.rb', line 21

def id
  @preview_inspector&.id || generate_id(lookup_path)
end

#itemsObject



67
68
69
# File 'lib/lookbook/preview.rb', line 67

def items
  examples.reject { |i| i.hidden? }
end

#labelObject



33
34
35
# File 'lib/lookbook/preview.rb', line 33

def label
  @preview_inspector&.label&.presence || lookup_path.split("/").last.titleize
end

#layoutObject



98
99
100
# File 'lib/lookbook/preview.rb', line 98

def layout
  @preview.instance_variable_get(:@layout)
end

#parent_collections_namesObject



90
91
92
# File 'lib/lookbook/preview.rb', line 90

def parent_collections_names
  File.dirname(path).split("/")
end

#preview_classObject



29
30
31
# File 'lib/lookbook/preview.rb', line 29

def preview_class
  @preview
end

#preview_class_nameObject



25
26
27
# File 'lib/lookbook/preview.rb', line 25

def preview_class_name
  @preview.name
end

#preview_pathsObject



121
122
123
# File 'lib/lookbook/preview.rb', line 121

def preview_paths
  PathUtils.normalize_all(Lookbook.config.preview_paths)
end

#rel_pathObject



75
76
77
# File 'lib/lookbook/preview.rb', line 75

def rel_path
  "#{name.underscore}.rb"
end

#typeObject



37
38
39
# File 'lib/lookbook/preview.rb', line 37

def type
  :preview
end

#url_pathObject



86
87
88
# File 'lib/lookbook/preview.rb', line 86

def url_path
  lookbook_inspect_path lookup_path
end