mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-16 17:30:43 -07:00
Refactor Condition so that it takes a parsed expression.
This commit is contained in:
@@ -3,7 +3,7 @@ module Liquid
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# c = Condition.new('1', '==', '1')
|
||||
# c = Condition.new(1, '==', 1)
|
||||
# c.evaluate #=> true
|
||||
#
|
||||
class Condition #:nodoc:
|
||||
@@ -28,9 +28,9 @@ module Liquid
|
||||
attr_accessor :left, :operator, :right
|
||||
|
||||
def initialize(left = nil, operator = nil, right = nil)
|
||||
@left = Expression.parse(left)
|
||||
@left = left
|
||||
@operator = operator
|
||||
@right = Expression.parse(right)
|
||||
@right = right
|
||||
@child_relation = nil
|
||||
@child_condition = nil
|
||||
end
|
||||
|
||||
@@ -8,7 +8,7 @@ module Liquid
|
||||
@blocks = []
|
||||
|
||||
if markup =~ Syntax
|
||||
@left = $1
|
||||
@left = Expression.parse($1)
|
||||
else
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.case".freeze))
|
||||
end
|
||||
@@ -58,7 +58,7 @@ module Liquid
|
||||
|
||||
markup = $2
|
||||
|
||||
block = Condition.new(@left, '=='.freeze, $1)
|
||||
block = Condition.new(@left, '=='.freeze, Expression.parse($1))
|
||||
block.attach(@nodelist)
|
||||
@blocks.push(block)
|
||||
end
|
||||
|
||||
@@ -60,14 +60,14 @@ module Liquid
|
||||
expressions = markup.scan(ExpressionsAndOperators)
|
||||
raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.pop =~ Syntax
|
||||
|
||||
condition = Condition.new($1, $2, $3)
|
||||
condition = Condition.new(Expression.parse($1), $2, Expression.parse($3))
|
||||
|
||||
while not expressions.empty?
|
||||
operator = expressions.pop.to_s.strip
|
||||
|
||||
raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.pop.to_s =~ Syntax
|
||||
|
||||
new_condition = Condition.new($1, $2, $3)
|
||||
new_condition = Condition.new(Expression.parse($1), $2, Expression.parse($3))
|
||||
raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless BOOLEAN_OPERATORS.include?(operator)
|
||||
new_condition.send(operator, condition)
|
||||
condition = new_condition
|
||||
@@ -92,9 +92,9 @@ module Liquid
|
||||
end
|
||||
|
||||
def parse_comparison(p)
|
||||
a = p.expression
|
||||
a = Expression.parse(p.expression)
|
||||
if op = p.consume?(:comparison)
|
||||
b = p.expression
|
||||
b = Expression.parse(p.expression)
|
||||
Condition.new(a, op, b)
|
||||
else
|
||||
Condition.new(a)
|
||||
|
||||
Reference in New Issue
Block a user