Class: Liquid::Condition
- Inherits:
-
Object
- Object
- Liquid::Condition
- 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
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
-
#attachment ⇒ Object
readonly
Returns the value of attribute attachment.
-
#left ⇒ Object
Returns the value of attribute left.
-
#operator ⇒ Object
Returns the value of attribute operator.
-
#right ⇒ Object
Returns the value of attribute right.
Class Method Summary collapse
Instance Method Summary collapse
- #and(condition) ⇒ Object
- #attach(attachment) ⇒ Object
- #else? ⇒ Boolean
- #evaluate(context = Context.new) ⇒ Object
-
#initialize(left = nil, operator = nil, right = nil) ⇒ Condition
constructor
A new instance of Condition.
- #inspect ⇒ Object
- #or(condition) ⇒ Object
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
#attachment ⇒ Object (readonly)
Returns the value of attribute attachment.
32 33 34 |
# File 'lib/liquid/condition.rb', line 32 def @attachment end |
#left ⇒ Object
Returns the value of attribute left.
33 34 35 |
# File 'lib/liquid/condition.rb', line 33 def left @left end |
#operator ⇒ Object
Returns the value of attribute operator.
33 34 35 |
# File 'lib/liquid/condition.rb', line 33 def operator @operator end |
#right ⇒ Object
Returns the value of attribute right.
33 34 35 |
# File 'lib/liquid/condition.rb', line 33 def right @right end |
Class Method Details
.operators ⇒ Object
28 29 30 |
# File 'lib/liquid/condition.rb', line 28 def self.operators @@operators end |
Instance Method Details
#and(condition) ⇒ Object
61 62 63 64 |
# File 'lib/liquid/condition.rb', line 61 def and(condition) @child_relation = :and @child_condition = condition end |
#attach(attachment) ⇒ Object
66 67 68 |
# File 'lib/liquid/condition.rb', line 66 def attach() @attachment = end |
#else? ⇒ Boolean
70 71 72 |
# File 'lib/liquid/condition.rb', line 70 def else? false end |
#evaluate(context = Context.new) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/liquid/condition.rb', line 43 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 |
#inspect ⇒ Object
74 75 76 |
# File 'lib/liquid/condition.rb', line 74 def inspect "#<Condition #{[@left, @operator, @right].compact.join(' '.freeze)}>" end |
#or(condition) ⇒ Object
56 57 58 59 |
# File 'lib/liquid/condition.rb', line 56 def or(condition) @child_relation = :or @child_condition = condition end |