Class: Liquid::VariableLookup
- Inherits:
-
Object
- Object
- Liquid::VariableLookup
- Defined in:
- lib/liquid/variable_lookup.rb
Constant Summary collapse
- SQUARE_BRACKETED =
/\A\[(.*)\]\z/m
- COMMAND_METHODS =
['size'.freeze, 'first'.freeze, 'last'.freeze]
Instance Attribute Summary collapse
-
#lookups ⇒ Object
readonly
Returns the value of attribute lookups.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #evaluate(context) ⇒ Object
-
#initialize(markup) ⇒ VariableLookup
constructor
A new instance of VariableLookup.
Constructor Details
#initialize(markup) ⇒ VariableLookup
Returns a new instance of VariableLookup.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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 |
Instance Attribute Details
#lookups ⇒ Object (readonly)
Returns the value of attribute lookups.
6 7 8 |
# File 'lib/liquid/variable_lookup.rb', line 6 def lookups @lookups end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/liquid/variable_lookup.rb', line 6 def name @name end |
Class Method Details
.parse(markup) ⇒ Object
8 9 10 |
# File 'lib/liquid/variable_lookup.rb', line 8 def self.parse(markup) new(markup) end |
Instance Method Details
#==(other) ⇒ Object
72 73 74 |
# File 'lib/liquid/variable_lookup.rb', line 72 def ==(other) self.class == other.class && state == other.state end |
#evaluate(context) ⇒ Object
34 35 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 65 66 67 68 69 70 |
# 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?(:key?) && object.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 or # raise an exception if `strict_variables` option is set to true else return nil unless context.strict_variables raise Liquid::UndefinedVariable, "undefined variable #{key}" end # If we are dealing with a drop here we have to object.context = context if object.respond_to?(:context=) end object end |