blank and empty as variable names

This commit is contained in:
Florian Weingarten
2015-06-04 12:30:50 -04:00
parent c319240174
commit 4c22cef341
4 changed files with 31 additions and 9 deletions

View File

@@ -78,17 +78,17 @@ module Liquid
private
def equal_variables(left, right)
if left.is_a?(Symbol)
if right.respond_to?(left)
return right.send(left.to_s)
if left.is_a?(Liquid::Expression::MethodLiteral)
if right.respond_to?(left.method_name)
return right.send(left.method_name)
else
return nil
end
end
if right.is_a?(Symbol)
if left.respond_to?(right)
return left.send(right.to_s)
if right.is_a?(Liquid::Expression::MethodLiteral)
if left.respond_to?(right.method_name)
return left.send(right.method_name)
else
return nil
end

View File

@@ -1,11 +1,24 @@
module Liquid
class Expression
class MethodLiteral
attr_reader :method_name, :to_s
def initialize(method_name, to_s)
@method_name = method_name
@to_s = to_s
end
def to_liquid
to_s
end
end
LITERALS = {
nil => nil, 'nil'.freeze => nil, 'null'.freeze => nil, ''.freeze => nil,
'true'.freeze => true,
'false'.freeze => false,
'blank'.freeze => :blank?,
'empty'.freeze => :empty?
'blank'.freeze => MethodLiteral.new(:blank?, '').freeze,
'empty'.freeze => MethodLiteral.new(:empty?, '').freeze
}
def self.parse(markup)

View File

@@ -24,6 +24,16 @@ class VariableTest < Minitest::Test
assert_equal '', template.render!
end
def test_using_blank_as_variable_name
template = Template.parse("{% assign foo = blank %}{{ foo }}")
assert_equal '', template.render!
end
def test_using_empty_as_variable_name
template = Template.parse("{% assign foo = empty %}{{ foo }}")
assert_equal '', template.render!
end
def test_hash_scoping
template = Template.parse(%({{ test.test }}))
assert_equal 'worked', template.render!('test' => { 'test' => 'worked' })

View File

@@ -110,4 +110,3 @@ class ErrorDrop < Liquid::Drop
raise Exception, 'exception'
end
end