Class: Rhales::ViewComposition

Inherits:
Object
  • Object
show all
Defined in:
lib/rhales/view_composition.rb

Overview

ViewComposition builds and represents the complete template dependency graph for a view render. It provides a data-agnostic, immutable representation of all templates (layout, view, partials) required for rendering.

This class is a key component in the two-pass rendering architecture, enabling server-side data aggregation before HTML generation.

Responsibilities:

  • Dependency Resolution: Recursively discovers and loads all partials

  • Structural Representation: Organizes templates into a traversable tree

  • Traversal Interface: Provides methods to iterate templates in render order

Key Characteristics:

  • Data-Agnostic: Knows nothing about runtime context or request data

  • Immutable: Once created, the composition is read-only

  • Cacheable: Can be cached in production for performance

Defined Under Namespace

Classes: CircularDependencyError, TemplateNotFoundError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_template_name, loader:) ⇒ ViewComposition

Returns a new instance of ViewComposition.



31
32
33
34
35
36
37
# File 'lib/rhales/view_composition.rb', line 31

def initialize(root_template_name, loader:)
  @root_template_name = root_template_name
  @loader             = loader
  @templates          = {}
  @dependencies       = {}
  @loading            = Set.new
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



29
30
31
# File 'lib/rhales/view_composition.rb', line 29

def dependencies
  @dependencies
end

#root_template_nameObject (readonly)

Returns the value of attribute root_template_name.



29
30
31
# File 'lib/rhales/view_composition.rb', line 29

def root_template_name
  @root_template_name
end

#templatesObject (readonly)

Returns the value of attribute templates.



29
30
31
# File 'lib/rhales/view_composition.rb', line 29

def templates
  @templates
end

Instance Method Details

#dependencies_of(template_name) ⇒ Object

Get direct dependencies of a template



82
83
84
# File 'lib/rhales/view_composition.rb', line 82

def dependencies_of(template_name)
  @dependencies[template_name] || []
end

#each_document_in_render_orderObject

Iterate through all documents in render order Layout -> View -> Partials (depth-first)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rhales/view_composition.rb', line 48

def each_document_in_render_order(&)
  return enum_for(:each_document_in_render_order) unless block_given?

  visited = Set.new

  # Process layout first if specified
  root_doc = @templates[@root_template_name]
  if root_doc && root_doc.layout
    layout_name = root_doc.layout
    if @templates[layout_name]
      yield_template_recursive(layout_name, visited, &)
    end
  end

  # Then process the root template and its dependencies
  yield_template_recursive(@root_template_name, visited, &)
end

#has_template?(name) ⇒ Boolean

Check if a template exists in the composition

Returns:

  • (Boolean)


72
73
74
# File 'lib/rhales/view_composition.rb', line 72

def has_template?(name)
  @templates.key?(name)
end

#resolve!Object

Resolve all template dependencies



40
41
42
43
44
# File 'lib/rhales/view_composition.rb', line 40

def resolve!
  load_template_recursive(@root_template_name)
  freeze_composition
  self
end

#template(name) ⇒ Object

Get a specific template by name



67
68
69
# File 'lib/rhales/view_composition.rb', line 67

def template(name)
  @templates[name]
end

#template_namesObject

Get all template names



77
78
79
# File 'lib/rhales/view_composition.rb', line 77

def template_names
  @templates.keys
end