Class: Lookbook::PreviewExample

Inherits:
Object
  • Object
show all
Includes:
Utils
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

Constructor Details

#initialize(name, preview) ⇒ PreviewExample

Returns a new instance of PreviewExample.



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

def initialize(name, preview)
  @name = name
  @preview = preview
  @example_inspector = CodeInspector.new("#{@preview.name}##{name}")
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/lookbook/preview_example.rb', line 5

def name
  @name
end

#previewObject (readonly)

Returns the value of attribute preview.



5
6
7
# File 'lib/lookbook/preview_example.rb', line 5

def preview
  @preview
end

Class Method Details

.allObject



68
69
70
# File 'lib/lookbook/preview_example.rb', line 68

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

.exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/lookbook/preview_example.rb', line 76

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

.find(path) ⇒ Object



72
73
74
# File 'lib/lookbook/preview_example.rb', line 72

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

Instance Method Details

#display_paramsObject



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

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



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

def id
  generate_id(@preview.id, name)
end

#labelObject



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

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

#matchersObject



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

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

#method_sourceObject



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

def method_source
  @example_inspector.source.split("\n")[1..-2].join("\n").strip_heredoc
end

#pathObject Also known as: lookup_path



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

def path
  "#{@preview.path}/#{name}"
end

#source_langObject



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

def source_lang
  Lookbook::Lang.find(:ruby)
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



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

def template_source(template_path)
  File.read(full_template_path(template_path))
end

#typeObject



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

def type
  :example
end