Class: Liquid::Context
- Inherits:
-
Object
- Object
- Liquid::Context
- Defined in:
- lib/liquid/context.rb
Overview
Context keeps the variable stack and resolves variables, as well as keywords
context['variable'] = 'testing'
context['variable'] #=> 'testing'
context['true'] #=> true
context['10.2232'] #=> 10.2232
context.stack do
context['bob'] = 'bobsen'
end
context['bob'] #=> nil class Context
Instance Attribute Summary collapse
-
#environments ⇒ Object
readonly
Returns the value of attribute environments.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#registers ⇒ Object
readonly
Returns the value of attribute registers.
-
#resource_limits ⇒ Object
readonly
Returns the value of attribute resource_limits.
-
#scopes ⇒ Object
readonly
Returns the value of attribute scopes.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#[]=(key, value) ⇒ Object
Only allow String, Numeric, Hash, Array, Proc, Boolean or
Liquid::Drop
. -
#add_filters(filters) ⇒ Object
Adds filters to this context.
- #clear_instance_assigns ⇒ Object
- #handle_error(e) ⇒ Object
-
#has_interrupt? ⇒ Boolean
are there any not handled interrupts?.
- #has_key?(key) ⇒ Boolean
-
#initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = {}) ⇒ Context
constructor
A new instance of Context.
- #invoke(method, *args) ⇒ Object
-
#merge(new_scopes) ⇒ Object
Merge a hash of variables in the current local scope.
-
#pop ⇒ Object
Pop from the stack.
-
#pop_interrupt ⇒ Object
pop an interrupt from the stack.
-
#push(new_scope = {}) ⇒ Object
Push new local scope on the stack.
-
#push_interrupt(e) ⇒ Object
push an interrupt to the stack.
- #resource_limits_reached? ⇒ Boolean
-
#stack(new_scope = {}) ⇒ Object
Pushes a new local scope on the stack, pops it at the end of the block.
- #strainer ⇒ Object
Constructor Details
#initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = {}) ⇒ Context
Returns a new instance of Context.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/liquid/context.rb', line 18 def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = {}) @environments = [environments].flatten @scopes = [(outer_scope || {})] @registers = registers @errors = [] @rethrow_errors = rethrow_errors @resource_limits = (resource_limits || {}).merge!({ :render_score_current => 0, :assign_score_current => 0 }) squash_instance_assigns_with_environments @interrupts = [] end |
Instance Attribute Details
#environments ⇒ Object (readonly)
Returns the value of attribute environments.
16 17 18 |
# File 'lib/liquid/context.rb', line 16 def environments @environments end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
16 17 18 |
# File 'lib/liquid/context.rb', line 16 def errors @errors end |
#registers ⇒ Object (readonly)
Returns the value of attribute registers.
16 17 18 |
# File 'lib/liquid/context.rb', line 16 def registers @registers end |
#resource_limits ⇒ Object (readonly)
Returns the value of attribute resource_limits.
16 17 18 |
# File 'lib/liquid/context.rb', line 16 def resource_limits @resource_limits end |
#scopes ⇒ Object (readonly)
Returns the value of attribute scopes.
16 17 18 |
# File 'lib/liquid/context.rb', line 16 def scopes @scopes end |
Instance Method Details
#[](key) ⇒ Object
126 127 128 |
# File 'lib/liquid/context.rb', line 126 def [](key) resolve(key) end |
#[]=(key, value) ⇒ Object
Only allow String, Numeric, Hash, Array, Proc, Boolean or Liquid::Drop
122 123 124 |
# File 'lib/liquid/context.rb', line 122 def []=(key, value) @scopes[0][key] = value end |
#add_filters(filters) ⇒ Object
Adds filters to this context.
Note that this does not register the filters with the main Template object. see Template.register_filter
for that
44 45 46 47 48 49 50 51 52 |
# File 'lib/liquid/context.rb', line 44 def add_filters(filters) filters = [filters].flatten.compact filters.each do |f| raise ArgumentError, "Expected module but got: #{f.class}" unless f.is_a?(Module) Strainer.add_known_filter(f) strainer.extend(f) end end |
#clear_instance_assigns ⇒ Object
117 118 119 |
# File 'lib/liquid/context.rb', line 117 def clear_instance_assigns @scopes[0] = {} end |
#handle_error(e) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/liquid/context.rb', line 69 def handle_error(e) errors.push(e) raise if @rethrow_errors case e when SyntaxError "Liquid syntax error: #{e.}" else "Liquid error: #{e.}" end end |
#has_interrupt? ⇒ Boolean
are there any not handled interrupts?
55 56 57 |
# File 'lib/liquid/context.rb', line 55 def has_interrupt? @interrupts.any? end |
#has_key?(key) ⇒ Boolean
130 131 132 |
# File 'lib/liquid/context.rb', line 130 def has_key?(key) resolve(key) != nil end |
#invoke(method, *args) ⇒ Object
81 82 83 |
# File 'lib/liquid/context.rb', line 81 def invoke(method, *args) strainer.invoke(method, *args) end |
#merge(new_scopes) ⇒ Object
Merge a hash of variables in the current local scope
92 93 94 |
# File 'lib/liquid/context.rb', line 92 def merge(new_scopes) @scopes[0].merge!(new_scopes) end |
#pop ⇒ Object
Pop from the stack. use Context#stack
instead
97 98 99 100 |
# File 'lib/liquid/context.rb', line 97 def pop raise ContextError if @scopes.size == 1 @scopes.shift end |
#pop_interrupt ⇒ Object
pop an interrupt from the stack
65 66 67 |
# File 'lib/liquid/context.rb', line 65 def pop_interrupt @interrupts.pop end |
#push(new_scope = {}) ⇒ Object
Push new local scope on the stack. use Context#stack
instead
86 87 88 89 |
# File 'lib/liquid/context.rb', line 86 def push(new_scope={}) @scopes.unshift(new_scope) raise StackLevelError, "Nesting too deep" if @scopes.length > 100 end |
#push_interrupt(e) ⇒ Object
push an interrupt to the stack. this interrupt is considered not handled.
60 61 62 |
# File 'lib/liquid/context.rb', line 60 def push_interrupt(e) @interrupts.push(e) end |
#resource_limits_reached? ⇒ Boolean
30 31 32 33 34 |
# File 'lib/liquid/context.rb', line 30 def resource_limits_reached? (@resource_limits[:render_length_limit] && @resource_limits[:render_length_current] > @resource_limits[:render_length_limit]) || (@resource_limits[:render_score_limit] && @resource_limits[:render_score_current] > @resource_limits[:render_score_limit] ) || (@resource_limits[:assign_score_limit] && @resource_limits[:assign_score_current] > @resource_limits[:assign_score_limit] ) end |
#stack(new_scope = {}) ⇒ Object
Pushes a new local scope on the stack, pops it at the end of the block
Example:
context.stack do
context['var'] = 'hi'
end
context['var] #=> nil
110 111 112 113 114 115 |
# File 'lib/liquid/context.rb', line 110 def stack(new_scope={}) push(new_scope) yield ensure pop end |