Class: TPPlus::Nodes::ExpressionNode
- Inherits:
-
Object
- Object
- TPPlus::Nodes::ExpressionNode
- Defined in:
- lib/tp_plus/nodes/expression_node.rb
Instance Attribute Summary collapse
-
#grouped ⇒ Object
Returns the value of attribute grouped.
-
#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
-
#initialize(left_op, op_string, right_op) ⇒ ExpressionNode
constructor
A new instance of ExpressionNode.
- #requires_mixed_logic?(context) ⇒ Boolean
- #string_val(context, options = {}) ⇒ Object
- #with_parens(string, context, options = {}) ⇒ Object
Constructor Details
#initialize(left_op, op_string, right_op) ⇒ ExpressionNode
Returns a new instance of ExpressionNode.
6 7 8 9 10 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 6 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
#grouped ⇒ Object
Returns the value of attribute grouped.
5 6 7 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 5 def grouped @grouped end |
#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
25 26 27 28 29 30 31 32 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 25 def boolean_result? case @op.string when "&&","||","!","==","<>",">",">=","<","<=" true else false end end |
#contains_expression? ⇒ Boolean
21 22 23 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 21 def contains_expression? [@left_op, @right_op].map {|op| op.is_a? ExpressionNode }.any? end |
#eval(context, options = {}) ⇒ Object
58 59 60 61 62 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 58 def eval(context,={}) [:force_parens] = true if @grouped with_parens(string_val(context, ), context, ) end |
#requires_mixed_logic?(context) ⇒ Boolean
12 13 14 15 16 17 18 19 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 12 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 |
# 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,options)}#{@left_op.eval(context)}" end else if @op.boolean? && [:opposite] "!#{@left_op.eval(context)}#{@op.eval(context,options)}!#{@right_op.eval(context)}" else "#{@left_op.eval(context)}#{@op.eval(context,options)}#{@right_op.eval(context)}" end end end |
#with_parens(string, context, options = {}) ⇒ Object
34 35 36 37 38 |
# File 'lib/tp_plus/nodes/expression_node.rb', line 34 def with_parens(string, context, ={}) return string unless [:force_parens] || [:as_condition] && requires_mixed_logic?(context) "(#{string})" end |