Class: Lookbook::RenderableEntity

Inherits:
Entity
  • Object
show all
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

Components collapse

Paths collapse

Instance Method Summary collapse

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).

Returns:

  • (Boolean)

    True if component



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_classClass

The associated component class (if the renderable is a component).

Returns:

  • (Class)

    The component class



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.

Returns:

  • (Boolean)

    True if no template is present



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

def inline?
  component? ? template_file_path.present? : false
end

#nameString

Parameter-safe entity name.

Returns:

  • (String)


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).

Returns:

  • (Boolean)

    True if component



83
84
85
# File 'lib/lookbook/entities/renderable_entity.rb', line 83

def template?
  !component?
end

#template_file_pathClass

Full path to the component template (if present) or view template/partial.

Returns:

  • (Class)

    The component class



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

#typeSymbol

Entity type identifier. Returns ‘:component` for components and `:template` for view templates/partials.

Returns:

  • (Symbol)

    The entity type



47
48
49
# File 'lib/lookbook/entities/renderable_entity.rb', line 47

def type
  component? ? :component : :template
end