Class: Liquid::Context

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false) ⇒ Context

Returns a new instance of Context.



18
19
20
21
22
23
24
25
# File 'lib/liquid/context.rb', line 18

def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false)
  @environments   = [environments].flatten
  @scopes         = [(outer_scope || {})]
  @registers      = registers
  @errors         = []
  @rethrow_errors = rethrow_errors
  squash_instance_assigns_with_environments
end

Instance Attribute Details

#environmentsObject (readonly)

Returns the value of attribute environments.



16
17
18
# File 'lib/liquid/context.rb', line 16

def environments
  @environments
end

#errorsObject (readonly)

Returns the value of attribute errors.



16
17
18
# File 'lib/liquid/context.rb', line 16

def errors
  @errors
end

#registersObject (readonly)

Returns the value of attribute registers.



16
17
18
# File 'lib/liquid/context.rb', line 16

def registers
  @registers
end

#scopesObject (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



111
112
113
# File 'lib/liquid/context.rb', line 111

def [](key)
  resolve(key)
end

#[]=(key, value) ⇒ Object

Only allow String, Numeric, Hash, Array, Proc, Boolean or Liquid::Drop



107
108
109
# File 'lib/liquid/context.rb', line 107

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



35
36
37
38
39
40
41
42
# File 'lib/liquid/context.rb', line 35

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

#clear_instance_assignsObject



102
103
104
# File 'lib/liquid/context.rb', line 102

def clear_instance_assigns
  @scopes[0] = {}
end

#handle_error(e) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/liquid/context.rb', line 44

def handle_error(e)
  errors.push(e)
  raise if @rethrow_errors

  case e
  when SyntaxError
    "Liquid syntax error: #{e.message}"
  else
    "Liquid error: #{e.message}"
  end
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/liquid/context.rb', line 115

def has_key?(key)
  resolve(key) != nil
end

#invoke(method, *args) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/liquid/context.rb', line 56

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



71
72
73
# File 'lib/liquid/context.rb', line 71

def merge(new_scopes)
  @scopes[0].merge!(new_scopes)
end

#popObject

Pop from the stack. use Context#stack instead

Raises:



76
77
78
79
# File 'lib/liquid/context.rb', line 76

def pop
  raise ContextError if @scopes.size == 1
  @scopes.shift
end

#push(new_scope = {}) ⇒ Object

Push new local scope on the stack. use Context#stack instead

Raises:



65
66
67
68
# File 'lib/liquid/context.rb', line 65

def push(new_scope={})
  raise StackLevelError, "Nesting too deep" if @scopes.length > 100
  @scopes.unshift(new_scope)
end

#stack(new_scope = {}, &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
99
100
# File 'lib/liquid/context.rb', line 89

def stack(new_scope={},&block)
  result = nil
  push(new_scope)

  begin
    result = yield
  ensure
    pop
  end

  result
end

#strainerObject



27
28
29
# File 'lib/liquid/context.rb', line 27

def strainer
  @strainer ||= Strainer.create(self)
end