Module: Lookbook::Preview

Includes:
Taggable
Defined in:
lib/lookbook/preview.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Taggable

#code_object, #lookbook_group, #lookbook_hidden?, #lookbook_notes

Class Method Details

.allObject



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

def all
  ViewComponent::Preview.all.sort_by(&:label)
end

.exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.find(path) ⇒ Object



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

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

Instance Method Details

#idObject



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

def id
  lookbook_path.tr("_", "-")
end

#lookbook_example(example_name) ⇒ Object Also known as: example



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

def lookbook_example(example_name)
  lookbook_examples.find { |m| m.name == example_name }
end

#lookbook_examplesObject Also known as: get_examples



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lookbook/preview.rb', line 22

def lookbook_examples
  return @lookbook_examples if @lookbook_examples.present?
  public_methods = public_instance_methods(false)
  public_method_objects = code_object.meths.filter { |m| public_methods.include?(m.name) }
  visible = public_method_objects.map { |m| PreviewExample.new(m.name.to_s, self) }.reject(&:hidden?)
  sorted = Lookbook.config.sort_examples ? visible.sort_by(&:label) : visible
  @lookbook_examples = []
  if code_object.groups.any?
    sorted.group_by { |m| m.group }.each do |name, examples|
      if name.nil?
        @lookbook_examples += examples
      else
        name = lookbook_label if name.strip == ""
        @lookbook_examples << PreviewGroup.new(name.underscore, self, examples)
      end
    end
  else
    @lookbook_examples = sorted
  end
  @lookbook_examples
end

#lookbook_full_pathObject

Examples::FooBarComponentPreview -> “/Users/myname/myapp/test/components/previews/examples/foo_bar_component_preview.rb” Examples::FooBarComponent::Preview -> “/Users/myname/myapp/test/components/previews/examples/foo_bar/component_preview.rb”



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

def lookbook_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

#lookbook_hierarchy_depthObject Also known as: hierarchy_depth



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

def lookbook_hierarchy_depth
  lookbook_path.split("/").size
end

#lookbook_idObject



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

def lookbook_id
  lookbook_path.tr("_", "-")
end

#lookbook_labelObject Also known as: label

Examples::FooBarComponent::Preview -> “Foo Bar”



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

def lookbook_label
  super.presence || lookbook_path.split("/").last.titleize
end

#lookbook_layoutObject



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

def lookbook_layout
  defined?(@layout) ? @layout : nil
end

#lookbook_nameObject

Examples::FooBarComponentPreview -> “Examples::FooBar” Examples::FooBarComponent::Preview -> “Examples::FooBar”



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

def lookbook_name
  name.chomp("ComponentPreview").chomp("Component::Preview").chomp("::")
end

#lookbook_parent_collectionsObject



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

def lookbook_parent_collections
  File.dirname(lookbook_path).split("/")
end

#lookbook_pathObject

Examples::FooBarComponentPreview -> “examples/foo_bar” Examples::FooBarComponent::Preview -> “examples/foo_bar”



52
53
54
# File 'lib/lookbook/preview.rb', line 52

def lookbook_path
  lookbook_name.underscore
end

#lookbook_typeObject Also known as: type



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

def lookbook_type
  :preview
end