Class: Liquid::VariableLookup
- Inherits:
-
Object
- Object
- Liquid::VariableLookup
show all
- Defined in:
- lib/liquid/variable_lookup.rb
Defined Under Namespace
Classes: ParseTreeVisitor
Constant Summary
collapse
- COMMAND_METHODS =
['size', 'first', 'last'].freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(markup, string_scanner = StringScanner.new(""), cache = nil) ⇒ VariableLookup
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/liquid/variable_lookup.rb', line 13
def initialize(markup, string_scanner = StringScanner.new(""), cache = nil)
lookups = markup.scan(VariableParser)
name = lookups.shift
if name&.start_with?('[') && name&.end_with?(']')
name = Expression.parse(
name[1..-2],
string_scanner,
cache,
)
end
@name = name
@lookups = lookups
@command_flags = 0
@lookups.each_index do |i|
lookup = lookups[i]
if lookup&.start_with?('[') && lookup&.end_with?(']')
lookups[i] = Expression.parse(
lookup[1..-2],
string_scanner,
cache,
)
elsif COMMAND_METHODS.include?(lookup)
@command_flags |= 1 << i
end
end
end
|
Instance Attribute Details
#lookups ⇒ Object
Returns the value of attribute lookups.
7
8
9
|
# File 'lib/liquid/variable_lookup.rb', line 7
def lookups
@lookups
end
|
#name ⇒ Object
Returns the value of attribute name.
7
8
9
|
# File 'lib/liquid/variable_lookup.rb', line 7
def name
@name
end
|
Class Method Details
.parse(markup, string_scanner = StringScanner.new(""), cache = nil) ⇒ Object
9
10
11
|
# File 'lib/liquid/variable_lookup.rb', line 9
def self.parse(markup, string_scanner = StringScanner.new(""), cache = nil)
new(markup, string_scanner, cache)
end
|
Instance Method Details
#==(other) ⇒ Object
88
89
90
|
# File 'lib/liquid/variable_lookup.rb', line 88
def ==(other)
self.class == other.class && state == other.state
end
|
#evaluate(context) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/liquid/variable_lookup.rb', line 47
def evaluate(context)
name = context.evaluate(@name)
object = context.find_variable(name)
@lookups.each_index do |i|
key = context.evaluate(@lookups[i])
key = Liquid::Utils.to_liquid_value(key)
if object.respond_to?(:[]) &&
((object.respond_to?(:key?) && object.key?(key)) ||
(object.respond_to?(:fetch) && key.is_a?(Integer)))
res = context.lookup_and_evaluate(object, key)
object = res.to_liquid
elsif lookup_command?(i) && object.respond_to?(key)
object = object.send(key).to_liquid
else
return nil unless context.strict_variables
raise Liquid::UndefinedVariable, "undefined variable #{key}"
end
object.context = context if object.respond_to?(:context=)
end
object
end
|
#lookup_command?(lookup_index) ⇒ Boolean
43
44
45
|
# File 'lib/liquid/variable_lookup.rb', line 43
def lookup_command?(lookup_index)
@command_flags & (1 << lookup_index) != 0
end
|