Class: Liquid::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid-rails/liquid_monkey_patch.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 Method Summary collapse

Instance Method Details

#find_variable(key) ⇒ Object

Fetches an object starting at the local scope and then moving up the hierachy



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/liquid-rails/liquid_monkey_patch.rb', line 36

def find_variable(key)
  # This was changed from find() to find_index() because this is a very hot
  # path and find_index() is optimized in MRI to reduce object allocation
  index = @scopes.find_index { |s| s.key?(key) }
  scope = @scopes[index] if index

  variable = nil

  if scope.nil?
    variable_is_nil = @environments.any? { |e| e.has_key?(key) && e[key].nil? }
    unless variable_is_nil
      @environments.each do |e|
        variable = lookup_and_evaluate(e, key)
        unless variable.nil?
          scope = e
          break
        end
      end
    end
  end

  scope ||= @environments.last || @scopes.last
  variable ||= lookup_and_evaluate(scope, key) unless variable_is_nil

  variable = variable.to_liquid
  variable.context = self if variable.respond_to?(:context=)

  variable
end