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: ParseTreeVisitor

Constant Summary collapse

@@operators =

:nodoc:

{
  '=='.freeze => ->(cond, left, right) {  cond.send(:equal_variables, left, right) },
  '!='.freeze => ->(cond, left, right) { !cond.send(:equal_variables, left, right) },
  '<>'.freeze => ->(cond, left, right) { !cond.send(:equal_variables, left, right) },
  '<'.freeze  => :<,
  '>'.freeze  => :>,
  '>='.freeze => :>=,
  '<='.freeze => :<=,
  'contains'.freeze => 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
  end
}

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.



35
36
37
38
39
40
41
# File 'lib/liquid/condition.rb', line 35

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.



32
33
34
# File 'lib/liquid/condition.rb', line 32

def attachment
  @attachment
end

#child_conditionObject (readonly)

Returns the value of attribute child_condition.



32
33
34
# File 'lib/liquid/condition.rb', line 32

def child_condition
  @child_condition
end

#leftObject

Returns the value of attribute left.



33
34
35
# File 'lib/liquid/condition.rb', line 33

def left
  @left
end

#operatorObject

Returns the value of attribute operator.



33
34
35
# File 'lib/liquid/condition.rb', line 33

def operator
  @operator
end

#rightObject

Returns the value of attribute right.



33
34
35
# File 'lib/liquid/condition.rb', line 33

def right
  @right
end

Class Method Details

.operatorsObject



28
29
30
# File 'lib/liquid/condition.rb', line 28

def self.operators
  @@operators
end

Instance Method Details

#and(condition) ⇒ Object



67
68
69
70
# File 'lib/liquid/condition.rb', line 67

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

#attach(attachment) ⇒ Object



72
73
74
# File 'lib/liquid/condition.rb', line 72

def attach(attachment)
  @attachment = attachment
end

#else?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/liquid/condition.rb', line 76

def else?
  false
end

#evaluate(context = Context.new) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/liquid/condition.rb', line 43

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

    case condition.child_relation
    when :or
      break if result
    when :and
      break unless result
    else
      break
    end
    condition = condition.child_condition
  end
  result
end

#inspectObject



80
81
82
# File 'lib/liquid/condition.rb', line 80

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

#or(condition) ⇒ Object



62
63
64
65
# File 'lib/liquid/condition.rb', line 62

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