Class: Lookbook::RenderableEntity
- Includes:
- LocatableEntity
- Defined in:
- lib/lookbook/entities/renderable_entity.rb
Overview
Represents the component or view template partial that is being rendered in a preview.
Identity collapse
-
#name ⇒ String
Parameter-safe entity name.
-
#type ⇒ Symbol
Entity type identifier.
Components collapse
-
#component? ⇒ Boolean
Whether or not the renderable is a component (as opposed to a view template/partial).
-
#component_class ⇒ Class
The associated component class (if the renderable is a component).
-
#inline? ⇒ Boolean
Whether or not the renderable is a component without a template.
-
#template? ⇒ Boolean
Whether or not the renderable is a view template/partial (as opposed to a component).
Paths collapse
-
#template_file_path ⇒ Class
Full path to the component template (if present) or view template/partial.
Instance Method Summary collapse
-
#initialize(identifier) ⇒ RenderableEntity
constructor
private
A new instance of RenderableEntity.
Methods inherited from Entity
#<=>, #id, #label, #lookup_path, #search_terms
Constructor Details
#initialize(identifier) ⇒ RenderableEntity
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 RenderableEntity.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/lookbook/entities/renderable_entity.rb', line 10 def initialize(identifier) @identifier = identifier @base_directories = Engine.component_paths @file_path = if component? PathUtils.determine_full_path("#{name}.rb", @base_directories).presence || begin locations = Where.is_class(identifier.constantize) dirs = @base_directories.sort_by { |d| d.size * -1 } lookup = locations.find do |loc| dirs.find { |d| loc[0].start_with?(d) } end lookup[0] if lookup end else PathUtils.determine_full_path(identifier, @base_directories) end unless @file_path && File.exist?(@file_path) raise Lookbook::Error, "The render target #{@identifier} was not found." end @lookup_path = PathUtils.to_lookup_path(relative_file_path) end |
Instance Method Details
#component? ⇒ Boolean
Whether or not the renderable is a component (as opposed to a view template/partial).
73 74 75 76 77 |
# File 'lib/lookbook/entities/renderable_entity.rb', line 73 def component? @identifier.first.upcase == @identifier.first && !@identifier.include?(".") && !@identifier.include?("/") end |
#component_class ⇒ Class
The associated component class (if the renderable is a component).
58 59 60 |
# File 'lib/lookbook/entities/renderable_entity.rb', line 58 def component_class @identifier.constantize if component? end |
#inline? ⇒ Boolean
Whether or not the renderable is a component without a template.
65 66 67 |
# File 'lib/lookbook/entities/renderable_entity.rb', line 65 def inline? component? ? template_file_path.present? : false end |
#name ⇒ String
Parameter-safe entity name.
38 39 40 |
# File 'lib/lookbook/entities/renderable_entity.rb', line 38 def name component? ? component_class.name.underscore : @identifier end |
#template? ⇒ Boolean
Whether or not the renderable is a view template/partial (as opposed to a component).
83 84 85 |
# File 'lib/lookbook/entities/renderable_entity.rb', line 83 def template? !component? end |
#template_file_path ⇒ Class
Full path to the component template (if present) or view template/partial.
95 96 97 |
# File 'lib/lookbook/entities/renderable_entity.rb', line 95 def template_file_path component? ? Dir.glob("#{directory_path}/#{file_name(true)}.*.erb").first : file_path end |
#type ⇒ Symbol
Entity type identifier. Returns ‘:component` for components and `:template` for view templates/partials.
47 48 49 |
# File 'lib/lookbook/entities/renderable_entity.rb', line 47 def type component? ? :component : :template end |