Class: Lookbook::PreviewExample

Inherits:
Entity
  • Object
show all
Defined in:
lib/lookbook/preview_example.rb

Constant Summary

Constants included from Utils

Utils::FRONTMATTER_REGEX, Utils::POSITION_PREFIX_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

#hidden?, #path, #position

Constructor Details

#initialize(name, preview, code_object) ⇒ PreviewExample

Returns a new instance of PreviewExample.



6
7
8
9
10
11
# File 'lib/lookbook/preview_example.rb', line 6

def initialize(name, preview, code_object)
  @name = name
  @preview = preview
  @example_inspector = SourceInspector.new(code_object)
  super("#{@preview.path}/#{name}")
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/lookbook/preview_example.rb', line 3

def name
  @name
end

#previewObject (readonly)

Returns the value of attribute preview.



3
4
5
# File 'lib/lookbook/preview_example.rb', line 3

def preview
  @preview
end

Class Method Details

.allObject



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

def all
  Preview.all.map { |preview| preview.examples }.flatten
end

.exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.find(path) ⇒ Object



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

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

Instance Method Details

#display_paramsObject



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

def display_params
  @preview.display_params.merge(@example_inspector.display_params)
end

#hierarchy_depthObject



54
55
56
# File 'lib/lookbook/preview_example.rb', line 54

def hierarchy_depth
  @preview.hierarchy_depth + 1
end

#idObject



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

def id
  @example_inspector&.id || generate_id(@preview.id, name)
end

#labelObject



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

def label
  @example_inspector.label.presence || name.titleize
end

#langObject



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

def lang
  Lookbook::Lang.find(:ruby)
end

#matchersObject



50
51
52
# File 'lib/lookbook/preview_example.rb', line 50

def matchers
  normalize_matchers(@preview.label, label)
end

#method_sourceObject



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

def method_source
  @example_inspector.source.sub(/^def \w+\s?(\([^)]+\))?/m, "").split("\n")[0..-2].join("\n").strip_heredoc.strip
end

#template_lang(template_path) ⇒ Object



42
43
44
# File 'lib/lookbook/preview_example.rb', line 42

def template_lang(template_path)
  Lookbook::Lang.guess(full_template_path(template_path)) || Lookbook::Lang.find(:html)
end

#template_source(template_path) ⇒ Object



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

def template_source(template_path)
  source_path = full_template_path(template_path)
  source_path ? File.read(source_path) : nil
end

#typeObject



46
47
48
# File 'lib/lookbook/preview_example.rb', line 46

def type
  :example
end

#url_pathObject



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

def url_path
  lookbook_inspect_path lookup_path
end