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
-
#scopes ⇒ Object
readonly
Returns the value of attribute scopes.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
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.
- #has_key?(key) ⇒ Boolean
-
#initialize(template) ⇒ 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.
- #registers ⇒ Object
-
#stack(&block) ⇒ Object
pushes a new local scope on the stack, pops it at the end of the block.
- #strainer ⇒ Object
Constructor Details
#initialize(template) ⇒ Context
Returns a new instance of Context.
22 23 24 25 |
# File 'lib/liquid/context.rb', line 22 def initialize(template) @template = template @scopes = [template.assigns] end |
Instance Attribute Details
#scopes ⇒ Object (readonly)
Returns the value of attribute scopes.
19 20 21 |
# File 'lib/liquid/context.rb', line 19 def scopes @scopes end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
20 21 22 |
# File 'lib/liquid/context.rb', line 20 def template @template end |
Instance Method Details
#[](key) ⇒ Object
97 98 99 |
# File 'lib/liquid/context.rb', line 97 def [](key) resolve(key) end |
#[]=(key, value) ⇒ Object
Only allow String, Numeric, Hash, Array, Proc, Boolean or Liquid::Drop
93 94 95 |
# File 'lib/liquid/context.rb', line 93 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
38 39 40 41 42 43 44 45 46 |
# File 'lib/liquid/context.rb', line 38 def add_filters(filters) filters = [filters].flatten.compact raise ArgumentError, "Expected module but got: #{filter_module.class}" unless filters.all? { |f| f.is_a?(Module)} filters.each do |f| strainer.extend(f) end end |
#has_key?(key) ⇒ Boolean
101 102 103 |
# File 'lib/liquid/context.rb', line 101 def has_key?(key) resolve(key) != nil end |
#invoke(method, *args) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/liquid/context.rb', line 48 def invoke(method, *args) if strainer.respond_to?(method) strainer.__send__(method, *args) else args.first end end |
#merge(new_scopes) ⇒ Object
merge a hash of variables in the current local scope
62 63 64 |
# File 'lib/liquid/context.rb', line 62 def merge(new_scopes) @scopes[0].merge!(new_scopes) end |
#pop ⇒ Object
pop from the stack. use Context#stack
instead
67 68 69 70 |
# File 'lib/liquid/context.rb', line 67 def pop raise ContextError if @scopes.size == 1 @scopes.shift end |
#push ⇒ Object
push new local scope on the stack. use Context#stack
instead
57 58 59 |
# File 'lib/liquid/context.rb', line 57 def push @scopes.unshift({}) end |
#registers ⇒ Object
31 32 33 |
# File 'lib/liquid/context.rb', line 31 def registers @template.registers 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
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/liquid/context.rb', line 81 def stack(&block) result = nil push begin result = yield ensure pop end result end |