Resolve literals faster.

This commit is contained in:
thedarkone
2011-01-28 21:16:22 -05:00
committed by Tobias Lütke
parent aa678302d6
commit a24049906b
+24 -22
View File
@@ -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