Class: Cyrel::Expression::Logical
- Defined in:
- lib/cyrel/expression/logical.rb
Overview
Represents a logical operation (AND, OR, XOR, NOT).
Instance Attribute Summary collapse
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#right ⇒ Object
readonly
Returns the value of attribute right.
Instance Method Summary collapse
-
#initialize(left, operator, right = nil) ⇒ Logical
constructor
A new instance of Logical.
-
#render(query) ⇒ String
Renders the logical expression.
Methods inherited from Base
#!=, #%, #&, #*, #+, #-, #/, #<, #<=, #==, #=~, #>, #>=, #^, #|
Constructor Details
#initialize(left, operator, right = nil) ⇒ Logical
Returns a new instance of Logical.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/cyrel/expression/logical.rb', line 12 def initialize(left, operator, right = nil) @operator = operator.to_s.upcase.to_sym # Ensure uppercase symbol raise ArgumentError, "Invalid logical operator: #{@operator}" unless %i[AND OR XOR NOT].include?(@operator) @left = Expression.coerce(left) @right = @operator == :NOT ? nil : Expression.coerce(right) raise ArgumentError, "Operator #{@operator} requires two operands." if @operator != :NOT && @right.nil? return unless @operator == :NOT && !right.nil? raise ArgumentError, 'Operator NOT requires only one operand.' end |
Instance Attribute Details
#left ⇒ Object (readonly)
Returns the value of attribute left.
7 8 9 |
# File 'lib/cyrel/expression/logical.rb', line 7 def left @left end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
7 8 9 |
# File 'lib/cyrel/expression/logical.rb', line 7 def operator @operator end |
#right ⇒ Object (readonly)
Returns the value of attribute right.
7 8 9 |
# File 'lib/cyrel/expression/logical.rb', line 7 def right @right end |
Instance Method Details
#render(query) ⇒ String
Renders the logical expression.
28 29 30 31 32 33 34 35 |
# File 'lib/cyrel/expression/logical.rb', line 28 def render(query) if @operator == :NOT "(#{@operator} #{@left.render(query)})" else # Parentheses ensure correct precedence "(#{@left.render(query)} #{@operator} #{@right.render(query)})" end end |