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
-
#assigns ⇒ Object
readonly
Returns the value of attribute assigns.
-
#registers ⇒ Object
Returns the value of attribute registers.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#[]=(key, value) ⇒ Object
Only allow String, Numeric, Hash, Array or
Liquid::Drop
. -
#add_filters(filter) ⇒ Object
adds filters to this context.
- #has_key?(key) ⇒ Boolean
-
#initialize(assigns = {}, registers = nil) ⇒ Context
constructor
A new instance of Context.
- #invoke(method, *args) ⇒ Object
-
#merge(new_assigns) ⇒ 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 = nil) ⇒ Context
Returns a new instance of Context.
22 23 24 25 |
# File 'lib/liquid/context.rb', line 22 def initialize(assigns = {}, registers = nil) @assigns = [assigns] @registers = registers || {} end |
Instance Attribute Details
#assigns ⇒ Object (readonly)
Returns the value of attribute assigns.
19 20 21 |
# File 'lib/liquid/context.rb', line 19 def assigns @assigns end |
#registers ⇒ Object
Returns the value of attribute registers.
20 21 22 |
# File 'lib/liquid/context.rb', line 20 def registers @registers end |
Instance Method Details
#[](key) ⇒ Object
87 88 89 |
# File 'lib/liquid/context.rb', line 87 def [](key) resolve(key) end |
#[]=(key, value) ⇒ Object
Only allow String, Numeric, Hash, Array or Liquid::Drop
83 84 85 |
# File 'lib/liquid/context.rb', line 83 def []=(key, value) @assigns[0][key] = value end |
#add_filters(filter) ⇒ Object
adds filters to this context. this does not register the filters with the main Template object. see Template.register_filter
for that
34 35 36 37 |
# File 'lib/liquid/context.rb', line 34 def add_filters(filter) return unless filter.is_a?(Module) strainer.extend(filter) end |
#has_key?(key) ⇒ Boolean
91 92 93 |
# File 'lib/liquid/context.rb', line 91 def has_key?(key) resolve(key) != nil end |
#invoke(method, *args) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/liquid/context.rb', line 39 def invoke(method, *args) if strainer.respond_to?(method) strainer.__send__(method, *args) else return args[0] end end |
#merge(new_assigns) ⇒ Object
merge a hash of variables in the current local scope
53 54 55 |
# File 'lib/liquid/context.rb', line 53 def merge(new_assigns) @assigns[0].merge!(new_assigns) end |
#pop ⇒ Object
pop from the stack. use Context#stack
instead
58 59 60 61 |
# File 'lib/liquid/context.rb', line 58 def pop raise ContextError if @assigns.size == 1 @assigns.shift end |
#push ⇒ Object
push new local scope on the stack. use Context#stack
instead
48 49 50 |
# File 'lib/liquid/context.rb', line 48 def push @assigns.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
72 73 74 75 76 77 78 79 80 |
# File 'lib/liquid/context.rb', line 72 def stack(&block) push begin result = yield ensure pop end result end |