From a24049906b8296c177ab0a6be7a011506a0545a8 Mon Sep 17 00:00:00 2001 From: thedarkone Date: Fri, 28 Jan 2011 23:22:47 +0100 Subject: [PATCH] Resolve literals faster. --- lib/liquid/context.rb | 46 ++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/liquid/context.rb b/lib/liquid/context.rb index 3b01e3da..f5fef8c8 100644 --- a/lib/liquid/context.rb +++ b/lib/liquid/context.rb @@ -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