Skip to content

Commit 03f61ee

Browse files
committed
Replace if tag relation parsing by Parser#expression
- Remove Condition#child_relation - Remove Condition#and - Remove Condition#or - Simplify Condition#evaluate
1 parent bf9b5e0 commit 03f61ee

File tree

3 files changed

+24
-58
lines changed

3 files changed

+24
-58
lines changed

lib/liquid/condition.rb

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,15 @@ module Liquid
99
# c.evaluate #=> true
1010
#
1111
class Condition # :nodoc:
12-
attr_reader :attachment, :child_condition
12+
attr_reader :attachment
1313
attr_accessor :left, :operator, :right
1414

1515
def initialize(left = nil)
1616
@left = left
17-
18-
@child_relation = nil
19-
@child_condition = nil
2017
end
2118

2219
def evaluate(context = deprecated_default_context)
23-
condition = self
24-
result = nil
25-
loop do
26-
result = context.evaluate(condition.left)
27-
28-
case condition.child_relation
29-
when :or
30-
break if Liquid::Utils.to_liquid_value(result)
31-
when :and
32-
break unless Liquid::Utils.to_liquid_value(result)
33-
else
34-
break
35-
end
36-
condition = condition.child_condition
37-
end
38-
result
39-
end
40-
41-
def or(condition)
42-
@child_relation = :or
43-
@child_condition = condition
44-
end
45-
46-
def and(condition)
47-
@child_relation = :and
48-
@child_condition = condition
20+
context.evaluate(left)
4921
end
5022

5123
def attach(attachment)
@@ -76,7 +48,6 @@ class ParseTreeVisitor < Liquid::ParseTreeVisitor
7648
def children
7749
[
7850
@node.left,
79-
@node.child_condition,
8051
@node.attachment
8152
].compact
8253
end

lib/liquid/tags/if.rb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,11 @@ def push_block(tag, markup)
7575

7676
def parse_markup(markup)
7777
p = @parse_context.new_parser(markup)
78-
condition = parse_binary_comparisons(p)
78+
condition = Condition.new(p.expression)
7979
p.consume(:end_of_string)
8080
condition
8181
end
8282

83-
def parse_binary_comparisons(p)
84-
condition = parse_comparison(p)
85-
first_condition = condition
86-
while (op = p.id?('and') || p.id?('or'))
87-
child_condition = parse_comparison(p)
88-
condition.send(op, child_condition)
89-
condition = child_condition
90-
end
91-
first_condition
92-
end
93-
94-
def parse_comparison(p)
95-
Condition.new(p.expression)
96-
end
97-
9883
class ParseTreeVisitor < Liquid::ParseTreeVisitor
9984
def children
10085
@node.blocks

test/unit/condition_unit_test.rb

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,31 +105,37 @@ def test_contains_with_string_left_operand_coerces_right_operand_to_string
105105
end
106106

107107
def test_or_condition
108-
false_expr = Parser.new('1 == 2').expression
109-
true_expr = Parser.new('1 == 1').expression
108+
false_expr = '1 == 2'
109+
true_expr = '1 == 1'
110110

111-
condition = Condition.new(false_expr)
111+
condition = Condition.new(expression(false_expr))
112112
assert_equal(false, condition.evaluate(Context.new))
113113

114-
condition.or(Condition.new(false_expr))
114+
condition = Condition.new(expression("#{false_expr} or #{false_expr}"))
115115
assert_equal(false, condition.evaluate(Context.new))
116116

117-
condition.or(Condition.new(true_expr))
117+
condition = Condition.new(expression("#{false_expr} or #{true_expr}"))
118+
assert_equal(true, condition.evaluate(Context.new))
119+
120+
condition = Condition.new(expression("#{true_expr} or #{false_expr}"))
118121
assert_equal(true, condition.evaluate(Context.new))
119122
end
120123

121124
def test_and_condition
122-
false_expr = Parser.new('1 == 2').expression
123-
true_expr = Parser.new('1 == 1').expression
125+
false_expr = '1 == 2'
126+
true_expr = '1 == 1'
124127

125-
condition = Condition.new(true_expr)
128+
condition = Condition.new(expression(true_expr))
126129
assert_equal(true, condition.evaluate(Context.new))
127130

128-
condition.and(Condition.new(true_expr))
129-
assert_equal(true, condition.evaluate(Context.new))
131+
condition = Condition.new(expression("#{true_expr} and #{false_expr}"))
132+
assert_equal(false, condition.evaluate(Context.new))
130133

131-
condition.and(Condition.new(false_expr))
134+
condition = Condition.new(expression("#{false_expr} and #{true_expr}"))
132135
assert_equal(false, condition.evaluate(Context.new))
136+
137+
condition = Condition.new(expression("#{true_expr} and #{true_expr}"))
138+
assert_equal(true, condition.evaluate(Context.new))
133139
end
134140

135141
def test_left_or_right_may_contain_operators
@@ -206,4 +212,8 @@ def assert_evaluates_argument_error(left, op, right)
206212
Condition.new(expr).evaluate(@context)
207213
end
208214
end
215+
216+
def expression(markup)
217+
Parser.new(markup).expression
218+
end
209219
end # ConditionTest

0 commit comments

Comments
 (0)