Class: Cyrel::Expression::Logical

Inherits:
Base
  • Object
show all
Defined in:
lib/cyrel/expression/logical.rb

Overview

Represents a logical operation (AND, OR, XOR, NOT).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#!=, #%, #&, #*, #+, #-, #/, #<, #<=, #==, #=~, #>, #>=, #^, #|

Constructor Details

#initialize(left, operator, right = nil) ⇒ Logical

Returns a new instance of Logical.

Parameters:

  • left (Cyrel::Expression::Base, Object)

    The left operand (or the single operand for NOT).

  • operator (Symbol)

    The logical operator symbol (:AND, :OR, :XOR, :NOT).

  • right (Cyrel::Expression::Base, Object, nil) (defaults to: nil)

    The right operand (nil for NOT).

Raises:

  • (ArgumentError)


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

#leftObject (readonly)

Returns the value of attribute left.



7
8
9
# File 'lib/cyrel/expression/logical.rb', line 7

def left
  @left
end

#operatorObject (readonly)

Returns the value of attribute operator.



7
8
9
# File 'lib/cyrel/expression/logical.rb', line 7

def operator
  @operator
end

#rightObject (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.

Parameters:

  • query (Cyrel::Query)

    The query object for rendering operands.

Returns:

  • (String)

    The Cypher string fragment (e.g., “((n.age > $p1) AND (n.status = $p2))”).



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