Class: Liquid::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid/condition.rb

Overview

Container for liquid nodes which conveniently wraps decision making logic

Example:

c = Condition.new(1, '==', 1)
c.evaluate #=> true

Direct Known Subclasses

ElseCondition

Defined Under Namespace

Classes: MethodLiteral, ParseTreeVisitor

Constant Summary collapse

@@operators =

:nodoc:

{
  '==' => ->(cond, left, right) {  cond.send(:equal_variables, left, right) },
  '!=' => ->(cond, left, right) { !cond.send(:equal_variables, left, right) },
  '<>' => ->(cond, left, right) { !cond.send(:equal_variables, left, right) },
  '<' => :<,
  '>' => :>,
  '>=' => :>=,
  '<=' => :<=,
  'contains' => lambda do |_cond, left, right|
    if left && right && left.respond_to?(:include?)
      right = right.to_s if left.is_a?(String)
      left.include?(right)
    else
      false
    end
  rescue Encoding::CompatibilityError
    # "✅".b.include?("✅") raises Encoding::CompatibilityError despite being materially equal
    left.b.include?(right.b)
  end,
}
@@method_literals =
{
  'blank' => MethodLiteral.new(:blank?, '').freeze,
  'empty' => MethodLiteral.new(:empty?, '').freeze,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left = nil, operator = nil, right = nil) ⇒ Condition

Returns a new instance of Condition.



58
59
60
61
62
63
64
65
# File 'lib/liquid/condition.rb', line 58

def initialize(left = nil, operator = nil, right = nil)
  @left     = left
  @operator = operator
  @right    = right

  @child_relation  = nil
  @child_condition = nil
end

Instance Attribute Details

#attachmentObject (readonly)

Returns the value of attribute attachment.



55
56
57
# File 'lib/liquid/condition.rb', line 55

def attachment
  @attachment
end

#child_conditionObject (readonly)

Returns the value of attribute child_condition.



55
56
57
# File 'lib/liquid/condition.rb', line 55

def child_condition
  @child_condition
end

#leftObject

Returns the value of attribute left.



56
57
58
# File 'lib/liquid/condition.rb', line 56

def left
  @left
end

#operatorObject

Returns the value of attribute operator.



56
57
58
# File 'lib/liquid/condition.rb', line 56

def operator
  @operator
end

#rightObject

Returns the value of attribute right.



56
57
58
# File 'lib/liquid/condition.rb', line 56

def right
  @right
end

Class Method Details

.operatorsObject



47
48
49
# File 'lib/liquid/condition.rb', line 47

def self.operators
  @@operators
end

.parse_expression(parse_context, markup) ⇒ Object



51
52
53
# File 'lib/liquid/condition.rb', line 51

def self.parse_expression(parse_context, markup)
  @@method_literals[markup] || parse_context.parse_expression(markup)
end

Instance Method Details

#and(condition) ⇒ Object



91
92
93
94
# File 'lib/liquid/condition.rb', line 91

def and(condition)
  @child_relation  = :and
  @child_condition = condition
end

#attach(attachment) ⇒ Object



96
97
98
# File 'lib/liquid/condition.rb', line 96

def attach(attachment)
  @attachment = attachment
end

#else?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/liquid/condition.rb', line 100

def else?
  false
end

#evaluate(context = deprecated_default_context) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/liquid/condition.rb', line 67

def evaluate(context = deprecated_default_context)
  condition = self
  result = nil
  loop do
    result = interpret_condition(condition.left, condition.right, condition.operator, context)

    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

#inspectObject



104
105
106
# File 'lib/liquid/condition.rb', line 104

def inspect
  "#<Condition #{[@left, @operator, @right].compact.join(' ')}>"
end

#or(condition) ⇒ Object



86
87
88
89
# File 'lib/liquid/condition.rb', line 86

def or(condition)
  @child_relation  = :or
  @child_condition = condition
end