Class: Lookbook::PreviewEntity

Inherits:
Entity
  • Object
show all
Includes:
AnnotatableEntity, LocatableEntity, NavigableEntity
Defined in:
lib/lookbook/entities/preview_entity.rb

Overview

Represents a preview class

Instance Attribute Summary collapse

Scenarios collapse

Render Targets collapse

URLs collapse

Instance Method Summary collapse

Methods inherited from Entity

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

Constructor Details

#initialize(code_object) ⇒ PreviewEntity

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of PreviewEntity.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lookbook/entities/preview_entity.rb', line 16

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 = preview_class.name.underscore.strip
    .gsub(/(_component_preview|_preview)(\..+)?$/, "")
    .gsub(/\/(component_preview|preview|component)(\..+)?$/, "")

  @lookup_path = PathUtils.to_lookup_path(cleaned_path)
end

Instance Attribute Details

#preview_classObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def preview_class
  @preview_class
end

Instance Method Details

#after_render_methodObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



162
163
164
# File 'lib/lookbook/entities/preview_entity.rb', line 162

def after_render_method
  fetch_config(:after_render)
end

#default_scenarioScenarioEntity

The scenario used when a preview is rendered without explicity specifying a scenario.

Examples:

:ruby

default_scenario_name = preview.default_scenario.name

Returns:



89
90
91
# File 'lib/lookbook/entities/preview_entity.rb', line 89

def default_scenario
  visible_scenarios.first
end

#display_optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



156
157
158
159
# File 'lib/lookbook/entities/preview_entity.rb', line 156

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

#file_name_baseObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



151
152
153
# File 'lib/lookbook/entities/preview_entity.rb', line 151

def file_name_base
  @_file_name_slug ||= file_name(true).gsub(/(_component_preview|component_preview|_preview|preview|component)$/, "")
end

#guess_render_targetsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



172
173
174
175
176
# File 'lib/lookbook/entities/preview_entity.rb', line 172

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

#hidden_scenariosArray<ScenarioEntity>

Get all scenarios defined in the preview class that are hidden (using the ‘@hidden` tag) and so will not show up in the navigation.

Examples:

:ruby

hidden_scenario_names = preview.hidden_scenarios.map(&:name)

Returns:



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

def hidden_scenarios
  @_hidden_scenarios ||= ScenarioCollection.new(scenarios.select(&:hidden?))
end

#inspect_pathString Also known as: url_path

The inspector URL path for this preview

Returns:

  • (String)

    URL path



128
129
130
# File 'lib/lookbook/entities/preview_entity.rb', line 128

def inspect_path
  lookbook_inspect_path(lookup_path)
end

#layoutObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



167
168
169
# File 'lib/lookbook/entities/preview_entity.rb', line 167

def layout
  preview_class.instance_variable_get(:@layout)
end

#preview_class_nameString

The name of the associated preview class

Returns:

  • (String)

    Class name



146
147
148
# File 'lib/lookbook/entities/preview_entity.rb', line 146

def preview_class_name
  preview_class.name
end

#preview_pathString

The standalone preview URL path for this preview

Returns:

  • (String)

    URL path



137
138
139
# File 'lib/lookbook/entities/preview_entity.rb', line 137

def preview_path
  lookbook_preview_path(lookup_path)
end

#render_targetObject Also known as: component

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



114
115
116
# File 'lib/lookbook/entities/preview_entity.rb', line 114

def render_target
  render_targets.first
end

#render_targetsArray<RenderableEntity> Also known as: components

All ‘render targets’ (i.e. components or partials) that are known to be rendered within the scenarios defined within this preview.

Render targets are guessed where possible (based on the preview class name) but can also be manually specified using the ‘@renders` tag.

Examples:

:ruby

"This preview renders: #{preview.render_targets.map(&:label).join(", ")}"

Returns:



107
108
109
# File 'lib/lookbook/entities/preview_entity.rb', line 107

def render_targets
  @_render_targets ||= RenderTargetCollection.new(scenarios.flat_map(&:render_targets).uniq(&:lookup_path))
end

#scenario(scenario_name) ⇒ ScenarioEntity? Also known as: example

Find a specific scenario by (i.e. method) name

Examples:

:ruby

default_scenario_preview_path = preview.scenario(:default).preview_path

Parameters:

  • scenario_name (Symbol, String)

    Name of the scenario

Returns:

  • (ScenarioEntity)

    The matching scenario, if found

  • (nil)

    if no matching scenario was found



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

def scenario(scenario_name)
  scenarios.find { |m| m.name == scenario_name.to_s }
end

#scenariosArray<ScenarioEntity> Also known as: examples

Get all scenarios defined in the preview class.

Examples:

:ruby

scenario_names = preview.scenarios.map(&:name)

Returns:



37
38
39
# File 'lib/lookbook/entities/preview_entity.rb', line 37

def scenarios
  @_scenarios ||= ScenarioCollection.new(load_scenarios)
end

#visible_scenariosArray<ScenarioEntity>

Get all scenarios defined in the preview class that have not been hidden (by using the ‘@hidden` tag).

Examples:

:ruby

visible_scenario_names = preview.visible_scenarios.map(&:name)

Returns:



66
67
68
# File 'lib/lookbook/entities/preview_entity.rb', line 66

def visible_scenarios
  @_visible_scenarios ||= ScenarioCollection.new(scenarios.select(&:visible?))
end