Class: Lookbook::Preview

Inherits:
Object
  • Object
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

Constructor Details

#initialize(preview) ⇒ Preview

Returns a new instance of Preview.



8
9
10
11
# File 'lib/lookbook/preview.rb', line 8

def initialize(preview)
  @preview = preview
  @preview_inspector = CodeInspector.new(@preview.name)
end

Class Method Details

.allObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/lookbook/preview.rb', line 103

def all
  load_previews if preview_files.size > ViewComponent::Preview.descendants.size

  return @previews if @previews.present?

  previews = ViewComponent::Preview.descendants.map do |p|
    new(p)
  rescue
    Rails.logger.error "[lookbook] error instantiating preview\n#{exception.full_message}"
  end

  if errors.any?
    errors.each do |error|
      Rails.logger.error "[lookbook] preview error\n#{error.full_message}\n"
    end
  end

  sorted_previews = previews.compact.sort_by { |preview| [preview.position, preview.label] }
  @previews ||= PreviewCollection.new(sorted_previews)
end

.clear_cacheObject



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

def clear_cache
  @previews = nil
end

.errorsObject



124
125
126
# File 'lib/lookbook/preview.rb', line 124

def errors
  @errors ||= []
end

.exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.find(path) ⇒ Object



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

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

Instance Method Details

#default_exampleObject



55
56
57
# File 'lib/lookbook/preview.rb', line 55

def default_example
  examples.first
end

#display_paramsObject



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

def display_params
  Lookbook.config.preview_display_params.deep_merge(@preview_inspector&.display_params)
end

#example(example_name) ⇒ Object



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

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

#examplesObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lookbook/preview.rb', line 33

def examples
  return @examples if @examples.present?
  public_methods = @preview.public_instance_methods(false)
  public_method_objects = @preview_inspector&.methods&.filter { |m| public_methods.include?(m.name) }
  examples = (public_method_objects || []).map { |m| PreviewExample.new(m.name.to_s, self) }
  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



63
64
65
66
67
68
# File 'lib/lookbook/preview.rb', line 63

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

#hierarchy_depthObject



78
79
80
# File 'lib/lookbook/preview.rb', line 78

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

#idObject



13
14
15
# File 'lib/lookbook/preview.rb', line 13

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

#labelObject



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

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

#layoutObject



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

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

#parent_collections_namesObject



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

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

#pathObject Also known as: lookup_path



59
60
61
# File 'lib/lookbook/preview.rb', line 59

def path
  preview_class_name(preview_class_basename(name))
end

#preview_classObject



17
18
19
# File 'lib/lookbook/preview.rb', line 17

def preview_class
  @preview.name
end

#preview_pathsObject



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

def preview_paths
  ViewComponent::Base.preview_paths
end

#typeObject



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

def type
  :preview
end