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

Constant Summary collapse

@@operators =

:nodoc:

{
  '==' => lambda { |cond, left, right|  cond.send(:equal_variables, left, right) },
  '!=' => lambda { |cond, left, right| !cond.send(:equal_variables, left, right) },
  '<>' => lambda { |cond, left, right| !cond.send(:equal_variables, left, right) },
  '<'  => :<,
  '>'  => :>,
  '>=' => :>=,
  '<=' => :<=,
  'contains' => lambda { |cond, left, right| left && right ? left.include?(right) : false }
}

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.



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

def initialize(left = nil, operator = nil, right = nil)
  @left, @operator, @right = left, operator, right
  @child_relation  = nil
  @child_condition = nil
end

Instance Attribute Details

#attachmentObject (readonly)

Returns the value of attribute attachment.



25
26
27
# File 'lib/liquid/condition.rb', line 25

def attachment
  @attachment
end

#leftObject

Returns the value of attribute left.



26
27
28
# File 'lib/liquid/condition.rb', line 26

def left
  @left
end

#operatorObject

Returns the value of attribute operator.



26
27
28
# File 'lib/liquid/condition.rb', line 26

def operator
  @operator
end

#rightObject

Returns the value of attribute right.



26
27
28
# File 'lib/liquid/condition.rb', line 26

def right
  @right
end

Class Method Details

.operatorsObject



21
22
23
# File 'lib/liquid/condition.rb', line 21

def self.operators
  @@operators
end

Instance Method Details

#and(condition) ⇒ Object



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

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

#attach(attachment) ⇒ Object



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

def attach(attachment)
  @attachment = attachment
end

#else?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/liquid/condition.rb', line 59

def else?
  false
end

#evaluate(context = Context.new) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/liquid/condition.rb', line 34

def evaluate(context = Context.new)
  result = interpret_condition(left, right, operator, context)

  case @child_relation
  when :or
    result || @child_condition.evaluate(context)
  when :and
    result && @child_condition.evaluate(context)
  else
    result
  end
end

#inspectObject



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

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

#or(condition) ⇒ Object



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

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