Class: TPPlus::Nodes::ExpressionNode
- Inherits:
-
Object
- Object
- TPPlus::Nodes::ExpressionNode
- Defined in:
- lib/tp_plus/nodes/expression_node.rb
Instance Attribute Summary collapse
-
#left_op ⇒ Object
readonly
Returns the value of attribute left_op.
-
#op ⇒ Object
readonly
Returns the value of attribute op.
-
#right_op ⇒ Object
readonly
Returns the value of attribute right_op.
Instance Method Summary collapse
- #boolean_result? ⇒ Boolean
- #contains_expression? ⇒ Boolean
- #eval(context, options = {}) ⇒ Object
- #grouped? ⇒ Boolean
-
#initialize(left_op, op_string, right_op) ⇒ ExpressionNode
constructor
A new instance of ExpressionNode.
- #requires_mixed_logic?(context) ⇒ Boolean
- #string_val(context, options = {}) ⇒ Object
Constructor Details
#initialize(left_op, op_string, right_op) ⇒ ExpressionNode
Returns a new instance of ExpressionNode.
5 6 7 8 9 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 5 def initialize(left_op, op_string, right_op) @left_op = left_op @op = OperatorNode.new(op_string) @right_op = right_op end |
Instance Attribute Details
#left_op ⇒ Object (readonly)
Returns the value of attribute left_op.
4 5 6 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 4 def left_op @left_op end |
#op ⇒ Object (readonly)
Returns the value of attribute op.
4 5 6 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 4 def op @op end |
#right_op ⇒ Object (readonly)
Returns the value of attribute right_op.
4 5 6 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 4 def right_op @right_op end |
Instance Method Details
#boolean_result? ⇒ Boolean
31 32 33 34 35 36 37 38 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 31 def boolean_result? case @op.string when "&&","||","!","==","<>",">",">=","<","<=" true else false end end |
#contains_expression? ⇒ Boolean
26 27 28 29 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 26 def contains_expression? [@left_op, @right_op].map {|op| op.is_a? ExpressionNode }.any? || [@left_op, @right_op].map { |op| op.is_a? ParenExpressionNode }.any? end |
#eval(context, options = {}) ⇒ Object
61 62 63 64 65 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 61 def eval(context,={}) [:force_parens] = true if grouped? string_val(context, ) end |
#grouped? ⇒ Boolean
11 12 13 14 15 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 11 def grouped? return false if @right_op.nil? # this is for NOT (!) operator @left_op.is_a?(ParenExpressionNode) || @right_op.is_a?(ParenExpressionNode) end |
#requires_mixed_logic?(context) ⇒ Boolean
17 18 19 20 21 22 23 24 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 17 def requires_mixed_logic?(context) contains_expression? || grouped? || [@op, @left_op, @right_op].map { |op| next if op.nil? op.requires_mixed_logic?(context) }.any? end |
#string_val(context, options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 40 def string_val(context, ={}) if @op.bang? # this is for skip conditions, which do not # support mixed logic if [:disable_mixed_logic] "#{@left_op.eval(context)}=OFF" else "#{@op.eval(context,)}#{@left_op.eval(context)}" end else if @op.boolean? # if operator is &&, || or !, we flip the operator and the operands "#{@left_op.eval(context,)}#{@op.eval(context, )}#{@right_op.eval(context, )}" else # flip the operator if options[:opposite] # flip the operands only if opposite and the operand is an expression "#{@left_op.eval(context, opposite: (@left_op.is_a?(ExpressionNode) && [:opposite]))}#{@op.eval(context, )}#{@right_op.eval(context, opposite: (@right_op.is_a?(ExpressionNode) && [:opposite]))}" end end end |