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:, config: nil) ⇒ ViewComposition

Returns a new instance of ViewComposition.



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

def initialize(root_template_name, loader:, config: nil)
  @root_template_name = root_template_name
  @loader             = loader
  @config             = config
  @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



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

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)



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

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

#extract_partials(parser) ⇒ Object (private)



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rhales/view_composition.rb', line 128

def extract_partials(parser)
  partials         = Set.new
  template_content = parser.section('template')

  return partials unless template_content

  # Extract partial references from template
  # Looking for {{> partial_name}} patterns
  template_content.scan(/\{\{>\s*([^\s}]+)\s*\}\}/) do |match|
    partials.add(match[0])
  end

  partials
end

#freeze_compositionObject (private)



159
160
161
162
163
164
165
# File 'lib/rhales/view_composition.rb', line 159

def freeze_composition
  @templates.freeze
  @dependencies.freeze
  @templates.each_value(&:freeze)
  @dependencies.each_value(&:freeze)
  freeze
end

#load_template_recursive(template_name, _parent_path = nil) ⇒ Object (private)



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rhales/view_composition.rb', line 90

def load_template_recursive(template_name, _parent_path = nil)
  # Check for circular dependencies
  if @loading.include?(template_name)
    raise CircularDependencyError, "Circular dependency detected: #{template_name} -> #{@loading.to_a.join(' -> ')}"
  end

  # Skip if already loaded
  return if @templates.key?(template_name)

  @loading.add(template_name)

  begin
    # Load template using the provided loader
    parser = @loader.call(template_name)

    unless parser
      raise TemplateNotFoundError, "Template not found: #{template_name}"
    end

    # Store the template
    @templates[template_name]    = parser
    @dependencies[template_name] = []

    # Extract and load partials
    extract_partials(parser).each do |partial_name|
      @dependencies[template_name] << partial_name
      load_template_recursive(partial_name, template_name)
    end

    # Load layout if specified and not already loaded
    if parser.layout && !@templates.key?(parser.layout)
      load_template_recursive(parser.layout, template_name)
    end
  ensure
    @loading.delete(template_name)
  end
end

#resolve!Object

Resolve all template dependencies



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

def resolve!
  load_template_recursive(@root_template_name)
  freeze_composition
  self
end

#template(name) ⇒ Object

Get a specific template by name



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

def template(name)
  @templates[name]
end

#template?(name) ⇒ Boolean

Check if a template exists in the composition

Returns:

  • (Boolean)


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

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

#template_namesObject

Get all template names



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

def template_names
  @templates.keys
end

#yield_template_recursive(template_name, visited) ⇒ Object (private)



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rhales/view_composition.rb', line 143

def yield_template_recursive(template_name, visited, &)
  return if visited.include?(template_name)

  visited.add(template_name)

  # First yield dependencies (partials)
  (@dependencies[template_name] || []).each do |dep_name|
    yield_template_recursive(dep_name, visited, &)
  end

  # Then yield the template itself
  if @templates[template_name]
    yield template_name, @templates[template_name]
  end
end