Class: Natsuzora::Context
- Inherits:
-
Object
- Object
- Natsuzora::Context
- Defined in:
- lib/natsuzora/context.rb
Overview
Manages variable scope and resolution during rendering.
Instance Method Summary collapse
-
#initialize(root_data) ⇒ Context
constructor
A new instance of Context.
- #pop_scope ⇒ Object
- #push_include_scope(bindings) ⇒ Object
- #push_scope(bindings = {}) ⇒ Object
- #resolve(variable) ⇒ Object
- #with_scope(bindings, include_scope: false) ⇒ Object
Constructor Details
Instance Method Details
#pop_scope ⇒ Object
39 40 41 |
# File 'lib/natsuzora/context.rb', line 39 def pop_scope @local_stack.pop end |
#push_include_scope(bindings) ⇒ Object
35 36 37 |
# File 'lib/natsuzora/context.rb', line 35 def push_include_scope(bindings) @local_stack.push(bindings) end |
#push_scope(bindings = {}) ⇒ Object
30 31 32 33 |
# File 'lib/natsuzora/context.rb', line 30 def push_scope(bindings = {}) validate_no_shadowing!(bindings) @local_stack.push(bindings) end |
#resolve(variable) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/natsuzora/context.rb', line 13 def resolve(variable) path = variable.path line = variable.line column = variable.column name = path.first raise UndefinedVariableError.new('Undefined variable: <empty path>', line: line, column: column) unless name value = resolve_name(name, line: line, column: column) path[1..].each do |segment| value = access_property(value, segment, line: line, column: column) end value end |
#with_scope(bindings, include_scope: false) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/natsuzora/context.rb', line 43 def with_scope(bindings, include_scope: false) if include_scope push_include_scope(bindings) else push_scope(bindings) end yield ensure pop_scope end |