Class: Rhales::ViewComposition
- Inherits:
-
Object
- Object
- Rhales::ViewComposition
- Includes:
- Utils::LoggingHelpers
- Defined in:
- lib/rhales/core/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
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#root_template_name ⇒ Object
readonly
Returns the value of attribute root_template_name.
-
#templates ⇒ Object
readonly
Returns the value of attribute templates.
Instance Method Summary collapse
-
#dependencies_of(template_name) ⇒ Object
Get direct dependencies of a template.
-
#each_document_in_render_order ⇒ Object
Iterate through all documents in render order Layout -> View -> Partials (depth-first).
-
#extract_partials(parser) ⇒ Object
private
-
#freeze_composition ⇒ Object
private
-
#initialize(root_template_name, loader:, config: nil) ⇒ ViewComposition
constructor
A new instance of ViewComposition.
-
#layout ⇒ Object
Get the layout for the root template (if any).
-
#load_template_recursive(template_name, parent_path = nil) ⇒ Object
private
-
#resolve! ⇒ Object
Resolve all template dependencies.
-
#template(name) ⇒ Object
Get a specific template by name.
-
#template?(name) ⇒ Boolean
Check if a template exists in the composition.
-
#template_names ⇒ Object
Get all template names.
-
#yield_template_recursive(template_name, visited) ⇒ Object
private
Methods included from Utils::LoggingHelpers
#format_value, #log_timed_operation, #log_with_metadata
Methods included from Utils
#now, #now_in_μs, #pretty_path
Constructor Details
#initialize(root_template_name, loader:, config: nil) ⇒ ViewComposition
Returns a new instance of ViewComposition.
35 36 37 38 39 40 41 42 |
# File 'lib/rhales/core/view_composition.rb', line 35 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
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
33 34 35 |
# File 'lib/rhales/core/view_composition.rb', line 33 def dependencies @dependencies end |
#root_template_name ⇒ Object (readonly)
Returns the value of attribute root_template_name.
33 34 35 |
# File 'lib/rhales/core/view_composition.rb', line 33 def root_template_name @root_template_name end |
#templates ⇒ Object (readonly)
Returns the value of attribute templates.
33 34 35 |
# File 'lib/rhales/core/view_composition.rb', line 33 def templates @templates end |
Instance Method Details
#dependencies_of(template_name) ⇒ Object
Get direct dependencies of a template
101 102 103 |
# File 'lib/rhales/core/view_composition.rb', line 101 def dependencies_of(template_name) @dependencies[template_name] || [] end |
#each_document_in_render_order ⇒ Object
Iterate through all documents in render order Layout -> View -> Partials (depth-first)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rhales/core/view_composition.rb', line 67 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)
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/rhales/core/view_composition.rb', line 198 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_composition ⇒ Object (private)
229 230 231 232 233 234 235 |
# File 'lib/rhales/core/view_composition.rb', line 229 def freeze_composition @templates.freeze @dependencies.freeze @templates.each_value(&:freeze) @dependencies.each_value(&:freeze) freeze end |
#layout ⇒ Object
Get the layout for the root template (if any)
106 107 108 109 110 111 |
# File 'lib/rhales/core/view_composition.rb', line 106 def layout root_template = @templates[@root_template_name] return nil unless root_template root_template.layout end |
#load_template_recursive(template_name, parent_path = nil) ⇒ Object (private)
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/rhales/core/view_composition.rb', line 115 def load_template_recursive(template_name, parent_path = nil) depth = @loading.size # Check for circular dependencies if @loading.include?(template_name) (Rhales.logger, :error, 'Circular dependency detected', template: template_name, dependency_chain: @loading.to_a, depth: depth ) raise CircularDependencyError, "Circular dependency detected: #{template_name} -> #{@loading.to_a.join(' -> ')}" end # Skip if already loaded (cache hit) if @templates.key?(template_name) Rhales.logger.debug("Template cache hit: template=#{template_name}") return end @loading.add(template_name) begin # Load template using the provided loader start_time = now_in_μs parser = @loader.call(template_name) load_duration = now_in_μs - start_time unless parser (Rhales.logger, :error, 'Template not found', template: template_name, parent: parent_path, depth: depth, search_duration: load_duration ) raise TemplateNotFoundError, "Template not found: #{template_name}" end # Store the template @templates[template_name] = parser @dependencies[template_name] = [] # Extract and load partials partials = extract_partials(parser) if partials.any? (Rhales.logger, :debug, 'Resolved partial', template: template_name, partials_found: partials, partial_count: partials.size, depth: depth, load_duration: load_duration ) end partials.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) (Rhales.logger, :debug, 'Layout resolution', template: template_name, layout: parser.layout, depth: depth ) load_template_recursive(parser.layout, template_name) end # Log successful template load (Rhales.logger, :debug, 'Template loaded', template: template_name, parent: parent_path, depth: depth, has_partials: partials.any?, has_layout: !parser.layout.nil?, load_duration: load_duration ) ensure @loading.delete(template_name) end end |
#resolve! ⇒ Object
Resolve all template dependencies
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rhales/core/view_composition.rb', line 45 def resolve! log_timed_operation(Rhales.logger, :debug, 'Template dependency resolution', root_template: @root_template_name ) do load_template_recursive(@root_template_name) freeze_composition # Log resolution results (Rhales.logger, :debug, 'Template composition resolved', root_template: @root_template_name, total_templates: @templates.size, total_dependencies: @dependencies.values.sum(&:size), partials: @dependencies.values.flatten.uniq, layout: layout ) self end end |
#template(name) ⇒ Object
Get a specific template by name
86 87 88 |
# File 'lib/rhales/core/view_composition.rb', line 86 def template(name) @templates[name] end |
#template?(name) ⇒ Boolean
Check if a template exists in the composition
91 92 93 |
# File 'lib/rhales/core/view_composition.rb', line 91 def template?(name) @templates.key?(name) end |
#template_names ⇒ Object
Get all template names
96 97 98 |
# File 'lib/rhales/core/view_composition.rb', line 96 def template_names @templates.keys end |
#yield_template_recursive(template_name, visited) ⇒ Object (private)
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/rhales/core/view_composition.rb', line 213 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 |