Class: Lookbook::PreviewEntity
- Includes:
- AnnotatableEntity, LocatableEntity, NavigableEntity
- Defined in:
- lib/lookbook/entities/preview_entity.rb
Overview
Represents a preview class
Instance Attribute Summary collapse
- #preview_class ⇒ Object readonly private
Scenarios collapse
-
#default_scenario ⇒ ScenarioEntity
The scenario used when a preview is rendered without explicity specifying a scenario.
-
#hidden_scenarios ⇒ Array<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.
-
#scenario(scenario_name) ⇒ ScenarioEntity?
(also: #example)
Find a specific scenario by (i.e. method) name.
-
#scenarios ⇒ Array<ScenarioEntity>
(also: #examples)
Get all scenarios defined in the preview class.
-
#visible_scenarios ⇒ Array<ScenarioEntity>
Get all scenarios defined in the preview class that have not been hidden (by using the ‘@hidden` tag).
Render Targets collapse
- #render_target ⇒ Object (also: #component) private
-
#render_targets ⇒ Array<RenderableEntity>
(also: #components)
All ‘render targets’ (i.e. components or partials) that are known to be rendered within the scenarios defined within this preview.
URLs collapse
-
#inspect_path ⇒ String
(also: #url_path)
The inspector URL path for this preview.
-
#preview_path ⇒ String
The standalone preview URL path for this preview.
Instance Method Summary collapse
- #after_render_method ⇒ Object private
- #display_options ⇒ Object private
- #file_name_base ⇒ Object private
- #guess_render_targets ⇒ Object private
-
#initialize(code_object) ⇒ PreviewEntity
constructor
private
A new instance of PreviewEntity.
- #layout ⇒ Object private
-
#preview_class_name ⇒ String
The name of the associated preview class.
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_class ⇒ Object (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_method ⇒ Object
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_scenario ⇒ ScenarioEntity
The scenario used when a preview is rendered without explicity specifying a scenario.
89 90 91 |
# File 'lib/lookbook/entities/preview_entity.rb', line 89 def default_scenario visible_scenarios.first end |
#display_options ⇒ Object
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 = Lookbook.config. .deep_merge(fetch_config(:display_options, {})) end |
#file_name_base ⇒ Object
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_targets ⇒ Object
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_scenarios ⇒ Array<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.
78 79 80 |
# File 'lib/lookbook/entities/preview_entity.rb', line 78 def hidden_scenarios @_hidden_scenarios ||= ScenarioCollection.new(scenarios.select(&:hidden?)) end |
#inspect_path ⇒ String Also known as: url_path
The inspector URL path for this preview
128 129 130 |
# File 'lib/lookbook/entities/preview_entity.rb', line 128 def inspect_path lookbook_inspect_path(lookup_path) end |
#layout ⇒ Object
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_name ⇒ String
The name of the associated preview class
146 147 148 |
# File 'lib/lookbook/entities/preview_entity.rb', line 146 def preview_class_name preview_class.name end |
#preview_path ⇒ String
The standalone preview URL path for this preview
137 138 139 |
# File 'lib/lookbook/entities/preview_entity.rb', line 137 def preview_path lookbook_preview_path(lookup_path) end |
#render_target ⇒ Object 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_targets ⇒ Array<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.
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
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 |
#scenarios ⇒ Array<ScenarioEntity> Also known as: examples
Get all scenarios defined in the preview class.
37 38 39 |
# File 'lib/lookbook/entities/preview_entity.rb', line 37 def scenarios @_scenarios ||= ScenarioCollection.new(load_scenarios) end |
#visible_scenarios ⇒ Array<ScenarioEntity>
Get all scenarios defined in the preview class that have not been hidden (by using the ‘@hidden` tag).
66 67 68 |
# File 'lib/lookbook/entities/preview_entity.rb', line 66 def visible_scenarios @_visible_scenarios ||= ScenarioCollection.new(scenarios.select(&:visible?)) end |