Class: Uniword::Template::TemplateContext
- Inherits:
-
Object
- Object
- Uniword::Template::TemplateContext
- Defined in:
- lib/uniword/template/template_context.rb
Overview
Manages rendering context and scope during template processing.
Handles nested scopes for loops and maintains the current data context. Provides clean scope isolation for loop iterations.
Responsibility: Context management only Single Responsibility Principle: Does NOT parse, render, or resolve
Instance Attribute Summary collapse
-
#additional_context ⇒ Object
readonly
Returns the value of attribute additional_context.
-
#root_data ⇒ Object
readonly
Returns the value of attribute root_data.
Instance Method Summary collapse
-
#create_resolver ⇒ VariableResolver
Create resolver for current scope.
-
#current_data ⇒ Object
Get current data scope.
-
#depth ⇒ Integer
Get current scope depth.
-
#initialize(root_data, additional_context = {}) ⇒ TemplateContext
constructor
Initialize context with data.
-
#inspect ⇒ String
Inspect context for debugging.
-
#nested? ⇒ Boolean
Check if in nested scope.
-
#pop_scope ⇒ Object
Pop scope from stack.
-
#push_scope(scope_data) ⇒ Object
Push new scope onto stack.
-
#reset ⇒ void
Reset context to root scope.
Constructor Details
#initialize(root_data, additional_context = {}) ⇒ TemplateContext
Initialize context with data
30 31 32 33 34 |
# File 'lib/uniword/template/template_context.rb', line 30 def initialize(root_data, additional_context = {}) @root_data = root_data @additional_context = additional_context @scope_stack = [root_data] end |
Instance Attribute Details
#additional_context ⇒ Object (readonly)
Returns the value of attribute additional_context.
24 25 26 |
# File 'lib/uniword/template/template_context.rb', line 24 def additional_context @additional_context end |
#root_data ⇒ Object (readonly)
Returns the value of attribute root_data.
24 25 26 |
# File 'lib/uniword/template/template_context.rb', line 24 def root_data @root_data end |
Instance Method Details
#create_resolver ⇒ VariableResolver
Create resolver for current scope
84 85 86 |
# File 'lib/uniword/template/template_context.rb', line 84 def create_resolver VariableResolver.new(current_data, @additional_context) end |
#current_data ⇒ Object
Get current data scope
39 40 41 |
# File 'lib/uniword/template/template_context.rb', line 39 def current_data @scope_stack.last end |
#depth ⇒ Integer
Get current scope depth
70 71 72 |
# File 'lib/uniword/template/template_context.rb', line 70 def depth @scope_stack.size end |
#inspect ⇒ String
Inspect context for debugging
98 99 100 |
# File 'lib/uniword/template/template_context.rb', line 98 def inspect "#<TemplateContext depth=#{depth} current=#{current_data.class.name}>" end |
#nested? ⇒ Boolean
Check if in nested scope
77 78 79 |
# File 'lib/uniword/template/template_context.rb', line 77 def nested? depth > 1 end |
#pop_scope ⇒ Object
Pop scope from stack
Returns to previous scope level.
60 61 62 63 64 65 |
# File 'lib/uniword/template/template_context.rb', line 60 def pop_scope # Always keep at least root scope return nil if @scope_stack.size <= 1 @scope_stack.pop end |
#push_scope(scope_data) ⇒ Object
Push new scope onto stack
Used when entering a loop or conditional block to create a new data context.
50 51 52 53 |
# File 'lib/uniword/template/template_context.rb', line 50 def push_scope(scope_data) @scope_stack.push(scope_data) scope_data end |
#reset ⇒ void
This method returns an undefined value.
Reset context to root scope
91 92 93 |
# File 'lib/uniword/template/template_context.rb', line 91 def reset @scope_stack = [@root_data] end |