class Liquid::VariableLookup
Constants
- COMMAND_METHODS
- SQUARE_BRACKETED
Attributes
lookups[R]
name[R]
Public Class Methods
new(markup)
click to toggle source
# File lib/liquid/variable_lookup.rb, line 12 def initialize(markup) lookups = markup.scan(VariableParser) name = lookups.shift if name =~ SQUARE_BRACKETED name = Expression.parse($1) end @name = name @lookups = lookups @command_flags = 0 @lookups.each_index do |i| lookup = lookups[i] if lookup =~ SQUARE_BRACKETED lookups[i] = Expression.parse($1) elsif COMMAND_METHODS.include?(lookup) @command_flags |= 1 << i end end end
parse(markup)
click to toggle source
# File lib/liquid/variable_lookup.rb, line 8 def self.parse(markup) new(markup) end
Public Instance Methods
==(other)
click to toggle source
# File lib/liquid/variable_lookup.rb, line 70 def ==(other) self.class == other.class && self.state == other.state end
evaluate(context)
click to toggle source
# File lib/liquid/variable_lookup.rb, line 34 def evaluate(context) name = context.evaluate(@name) object = context.find_variable(name) @lookups.each_index do |i| key = context.evaluate(@lookups[i]) # If object is a hash- or array-like object we look for the # presence of the key and if its available we return it if object.respond_to?(:[]) && ((object.respond_to?(:has_key?) && object.has_key?(key)) || (object.respond_to?(:fetch) && key.is_a?(Integer))) # if its a proc we will replace the entry with the proc res = context.lookup_and_evaluate(object, key) object = res.to_liquid # Some special cases. If the part wasn't in square brackets and # no key with the same name was found we interpret following calls # as commands and call them on the current object elsif @command_flags & (1 << i) != 0 && object.respond_to?(key) object = object.send(key).to_liquid # No key was present with the desired value and it wasn't one of the directly supported # keywords either. The only thing we got left is to return nil else return nil end # If we are dealing with a drop here we have to object.context = context if object.respond_to?(:context=) end object end
Protected Instance Methods
state()
click to toggle source
# File lib/liquid/variable_lookup.rb, line 76 def state [@name, @lookups, @command_flags] end