mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Resolve literals faster.
This commit is contained in:
+24
-22
@@ -111,6 +111,14 @@ module Liquid
|
||||
end
|
||||
|
||||
private
|
||||
LITERALS = {
|
||||
nil => nil, 'nil' => nil, 'null' => nil, '' => nil,
|
||||
'true' => true,
|
||||
'false' => false,
|
||||
'blank' => :blank?,
|
||||
'empty' => :empty?
|
||||
}
|
||||
|
||||
# Look up variable, either resolve directly after considering the name. We can directly handle
|
||||
# Strings, digits, floats and booleans (true,false).
|
||||
# If no match is made we lookup the variable in the current scope and
|
||||
@@ -120,29 +128,23 @@ module Liquid
|
||||
# Example:
|
||||
# products == empty #=> products.empty?
|
||||
def resolve(key)
|
||||
case key
|
||||
when nil, 'nil', 'null', ''
|
||||
nil
|
||||
when 'true'
|
||||
true
|
||||
when 'false'
|
||||
false
|
||||
when 'blank'
|
||||
:blank?
|
||||
when 'empty'
|
||||
:empty?
|
||||
when /^'(.*)'$/ # Single quoted strings
|
||||
$1.to_s
|
||||
when /^"(.*)"$/ # Double quoted strings
|
||||
$1.to_s
|
||||
when /^(\d+)$/ # Integer and floats
|
||||
$1.to_i
|
||||
when /^\((\S+)\.\.(\S+)\)$/ # Ranges
|
||||
(resolve($1).to_i..resolve($2).to_i)
|
||||
when /^(\d[\d\.]+)$/ # Floats
|
||||
$1.to_f
|
||||
if LITERALS.key?(key)
|
||||
LITERALS[key]
|
||||
else
|
||||
variable(key)
|
||||
case key
|
||||
when /^'(.*)'$/ # Single quoted strings
|
||||
$1.to_s
|
||||
when /^"(.*)"$/ # Double quoted strings
|
||||
$1.to_s
|
||||
when /^(\d+)$/ # Integer and floats
|
||||
$1.to_i
|
||||
when /^\((\S+)\.\.(\S+)\)$/ # Ranges
|
||||
(resolve($1).to_i..resolve($2).to_i)
|
||||
when /^(\d[\d\.]+)$/ # Floats
|
||||
$1.to_f
|
||||
else
|
||||
variable(key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user