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
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#registers ⇒ Object
readonly
Returns the value of attribute registers.
-
#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.
- #handle_error(e) ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(assigns = {}, registers = {}, rethrow_errors = false) ⇒ 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.
-
#push ⇒ Object
push new local scope on the stack.
-
#stack(&block) ⇒ Object
pushes a new local scope on the stack, pops it at the end of the block.
- #strainer ⇒ Object
Constructor Details
#initialize(assigns = {}, registers = {}, rethrow_errors = false) ⇒ Context
Returns a new instance of Context.
19 20 21 22 23 24 |
# File 'lib/liquid/context.rb', line 19 def initialize(assigns = {}, registers = {}, rethrow_errors = false) @scopes = [(assigns || {})] @registers = registers @errors = [] @rethrow_errors = rethrow_errors end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
17 18 19 |
# File 'lib/liquid/context.rb', line 17 def errors @errors end |
#registers ⇒ Object (readonly)
Returns the value of attribute registers.
17 18 19 |
# File 'lib/liquid/context.rb', line 17 def registers @registers 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
105 106 107 |
# File 'lib/liquid/context.rb', line 105 def [](key) resolve(key) end |
#[]=(key, value) ⇒ Object
Only allow String, Numeric, Hash, Array, Proc, Boolean or Liquid::Drop
101 102 103 |
# File 'lib/liquid/context.rb', line 101 def []=(key, value) @scopes[0][key] = value end |
#add_filters(filters) ⇒ Object
adds filters to this context. this does not register the filters with the main Template object. see Template.register_filter
for that
33 34 35 36 37 38 39 40 |
# File 'lib/liquid/context.rb', line 33 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.extend(f) end end |
#handle_error(e) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/liquid/context.rb', line 42 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_key?(key) ⇒ Boolean
109 110 111 |
# File 'lib/liquid/context.rb', line 109 def has_key?(key) resolve(key) != nil end |
#invoke(method, *args) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/liquid/context.rb', line 55 def invoke(method, *args) if strainer.respond_to?(method) strainer.__send__(method, *args) else raise FilterNotFound, "Filter '#{method}' not found" end end |
#merge(new_scopes) ⇒ Object
merge a hash of variables in the current local scope
70 71 72 |
# File 'lib/liquid/context.rb', line 70 def merge(new_scopes) @scopes[0].merge!(new_scopes) end |
#pop ⇒ Object
pop from the stack. use Context#stack
instead
75 76 77 78 |
# File 'lib/liquid/context.rb', line 75 def pop raise ContextError if @scopes.size == 1 @scopes.shift end |
#push ⇒ Object
push new local scope on the stack. use Context#stack
instead
64 65 66 67 |
# File 'lib/liquid/context.rb', line 64 def push raise StackLevelError, "Nesting too deep" if @scopes.length > 100 @scopes.unshift({}) end |
#stack(&block) ⇒ 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
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/liquid/context.rb', line 89 def stack(&block) result = nil push begin result = yield ensure pop end result end |