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
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 end, }
- @@method_literals =
{ 'blank' => MethodLiteral.new(:blank?, '').freeze, 'empty' => MethodLiteral.new(:empty?, '').freeze, }
Instance Attribute Summary collapse
-
#attachment ⇒ Object
readonly
Returns the value of attribute attachment.
-
#child_condition ⇒ Object
readonly
Returns the value of attribute child_condition.
-
#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 = deprecated_default_context) ⇒ 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.
55 56 57 58 59 60 61 62 |
# File 'lib/liquid/condition.rb', line 55 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.
52 53 54 |
# File 'lib/liquid/condition.rb', line 52 def @attachment end |
#child_condition ⇒ Object (readonly)
Returns the value of attribute child_condition.
52 53 54 |
# File 'lib/liquid/condition.rb', line 52 def child_condition @child_condition end |
#left ⇒ Object
Returns the value of attribute left.
53 54 55 |
# File 'lib/liquid/condition.rb', line 53 def left @left end |
#operator ⇒ Object
Returns the value of attribute operator.
53 54 55 |
# File 'lib/liquid/condition.rb', line 53 def operator @operator end |
#right ⇒ Object
Returns the value of attribute right.
53 54 55 |
# File 'lib/liquid/condition.rb', line 53 def right @right end |
Class Method Details
.operators ⇒ Object
44 45 46 |
# File 'lib/liquid/condition.rb', line 44 def self.operators @@operators end |
.parse_expression(parse_context, markup) ⇒ Object
48 49 50 |
# File 'lib/liquid/condition.rb', line 48 def self.parse_expression(parse_context, markup) @@method_literals[markup] || parse_context.parse_expression(markup) end |
Instance Method Details
#and(condition) ⇒ Object
88 89 90 91 |
# File 'lib/liquid/condition.rb', line 88 def and(condition) @child_relation = :and @child_condition = condition end |
#attach(attachment) ⇒ Object
93 94 95 |
# File 'lib/liquid/condition.rb', line 93 def attach() @attachment = end |
#else? ⇒ Boolean
97 98 99 |
# File 'lib/liquid/condition.rb', line 97 def else? false end |
#evaluate(context = deprecated_default_context) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/liquid/condition.rb', line 64 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 result when :and break unless result else break end condition = condition.child_condition end result end |
#inspect ⇒ Object
101 102 103 |
# File 'lib/liquid/condition.rb', line 101 def inspect "#<Condition #{[@left, @operator, @right].compact.join(' ')}>" end |
#or(condition) ⇒ Object
83 84 85 86 |
# File 'lib/liquid/condition.rb', line 83 def or(condition) @child_relation = :or @child_condition = condition end |