Class: Lookbook::Preview

Inherits:
Entity
  • Object
show all
Includes:
Annotatable, Locatable, Navigable
Defined in:
lib/lookbook/entities/preview.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

#<=>, #id, #label, #lookup_path, #name, #type

Constructor Details

#initialize(code_object) ⇒ Preview

Returns a new instance of Preview.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lookbook/entities/preview.rb', line 11

def initialize(code_object)
  @code_object = code_object
  @preview_class = code_object.path.constantize
  @file_path = Pathname(code_object.file)
  @base_directories = Engine.preview_paths

  cleaned_path = relative_file_path.to_s
    .gsub(/\/(component_preview|preview)(\..*)$/, "")
    .gsub(/(_component_preview|_preview)(\..*)$/, "")

  @lookup_path = PathUtils.to_lookup_path(cleaned_path)
end

Instance Attribute Details

#preview_classObject (readonly)

Returns the value of attribute preview_class.



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

def preview_class
  @preview_class
end

Instance Method Details

#componentObject



44
45
46
# File 'lib/lookbook/entities/preview.rb', line 44

def component
  components.first
end

#componentsObject



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

def components
  @_components ||= ComponentCollection.new(examples.flat_map(&:components).uniq(&:path))
end

#default_exampleObject



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

def default_example
  visible_examples.first
end

#display_optionsObject



48
49
50
51
# File 'lib/lookbook/entities/preview.rb', line 48

def display_options
  global_options = Lookbook.config.preview_display_options
  global_options.deep_merge(fetch_config(:display_options, {}))
end

#example(example_name) ⇒ Object



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

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

#examplesObject



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

def examples
  @_examples ||= PreviewExampleCollection.new(load_examples)
end

#guess_componentsObject



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

def guess_components
  [preview_class.name.chomp("Preview").constantize]
rescue
  []
end

#layoutObject



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

def layout
  preview_class.instance_variable_get(:@layout)
end

#preview_class_nameObject



61
62
63
# File 'lib/lookbook/entities/preview.rb', line 61

def preview_class_name
  preview_class.name
end

#url_pathObject



57
58
59
# File 'lib/lookbook/entities/preview.rb', line 57

def url_path
  lookbook_inspect_path(path)
end

#visible_examplesObject



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

def visible_examples
  @_visible_examples ||= PreviewExampleCollection.new(examples.select(&:visible?))
end