mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-16 01:10:41 -07:00
Remove Condition#{child_relation,and,or}
- Remove Condition#child_relation - Remove Condition#and - Remove Condition#or - Simplify Condition#evaluate This logic was moved to the Parser & BinaryExpression
This commit is contained in:
+3
-31
@@ -9,43 +9,15 @@ module Liquid
|
||||
# c.evaluate #=> true
|
||||
#
|
||||
class Condition # :nodoc:
|
||||
attr_reader :attachment, :child_condition
|
||||
attr_reader :attachment
|
||||
attr_accessor :left
|
||||
|
||||
def initialize(left = nil)
|
||||
@left = left
|
||||
|
||||
@child_relation = nil
|
||||
@child_condition = nil
|
||||
end
|
||||
|
||||
def evaluate(context = deprecated_default_context)
|
||||
condition = self
|
||||
result = nil
|
||||
loop do
|
||||
result = context.evaluate(condition.left)
|
||||
|
||||
case condition.child_relation
|
||||
when :or
|
||||
break if Liquid::Utils.to_liquid_value(result)
|
||||
when :and
|
||||
break unless Liquid::Utils.to_liquid_value(result)
|
||||
else
|
||||
break
|
||||
end
|
||||
condition = condition.child_condition
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
def or(condition)
|
||||
@child_relation = :or
|
||||
@child_condition = condition
|
||||
end
|
||||
|
||||
def and(condition)
|
||||
@child_relation = :and
|
||||
@child_condition = condition
|
||||
context.evaluate(left)
|
||||
end
|
||||
|
||||
def attach(attachment)
|
||||
@@ -65,6 +37,7 @@ module Liquid
|
||||
attr_reader :child_relation
|
||||
|
||||
private
|
||||
|
||||
def deprecated_default_context
|
||||
warn("DEPRECATION WARNING: Condition#evaluate without a context argument is deprecated " \
|
||||
"and will be removed from Liquid 6.0.0.")
|
||||
@@ -75,7 +48,6 @@ module Liquid
|
||||
def children
|
||||
[
|
||||
@node.left,
|
||||
@node.child_condition,
|
||||
@node.attachment
|
||||
].compact
|
||||
end
|
||||
|
||||
+1
-16
@@ -75,26 +75,11 @@ module Liquid
|
||||
|
||||
def parse_markup(markup)
|
||||
p = @parse_context.new_parser(markup)
|
||||
condition = parse_binary_comparisons(p)
|
||||
condition = Condition.new(p.expression)
|
||||
p.consume(:end_of_string)
|
||||
condition
|
||||
end
|
||||
|
||||
def parse_binary_comparisons(p)
|
||||
condition = parse_comparison(p)
|
||||
first_condition = condition
|
||||
while (op = p.id?('and') || p.id?('or'))
|
||||
child_condition = parse_comparison(p)
|
||||
condition.send(op, child_condition)
|
||||
condition = child_condition
|
||||
end
|
||||
first_condition
|
||||
end
|
||||
|
||||
def parse_comparison(p)
|
||||
Condition.new(p.expression)
|
||||
end
|
||||
|
||||
class ParseTreeVisitor < Liquid::ParseTreeVisitor
|
||||
def children
|
||||
@node.blocks
|
||||
|
||||
Reference in New Issue
Block a user