Class: Cyrel::Expression::Comparison

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

Overview

Represents a comparison operation (e.g., =, <>, <, >, <=, >=, =~, IN, STARTS WITH, etc.).

Constant Summary collapse

OPERATOR_MAP =

Mapping from Ruby-friendly symbols to Cypher operators

{
  :'=' => '=',
  :'==' => '=', # Allow Ruby style equality check
  :'!=' => '<>',
  :'<>' => '<>', # Allow SQL style inequality
  :< => '<',
  :<= => '<=',
  :> => '>',
  :>= => '>=',
  :'=~' => '=~', # Regex
  :IN => 'IN',
  :'STARTS WITH' => 'STARTS WITH',
  :'ENDS WITH' => 'ENDS WITH',
  :CONTAINS => 'CONTAINS',
  :'IS NULL' => 'IS NULL',
  :'IS NOT NULL' => 'IS NOT NULL'
  # Add other Cypher comparison operators as needed
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

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

Returns a new instance of Comparison.

Parameters:

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

    The left operand.

  • operator (Symbol, String)

    The comparison operator (e.g., :>, :‘=’, :IN).

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

    The right operand (nil for unary ops like IS NULL).

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cyrel/expression/comparison.rb', line 32

def initialize(left, operator, right = nil)
  @left = Expression.coerce(left)
  @operator_sym = operator.to_sym
  @cypher_operator = OPERATOR_MAP[@operator_sym] || operator.to_s.upcase # Fallback for unmapped/custom
  raise ArgumentError, "Unknown comparison operator: #{operator}" unless @cypher_operator

  # Handle unary operators like IS NULL / IS NOT NULL
  @right = if right.nil? && (@operator_sym == :'IS NULL' || @operator_sym == :'IS NOT NULL')
             nil
           else
             Expression.coerce(right)
           end
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



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

def left
  @left
end

#operatorObject (readonly)

Returns the value of attribute operator.



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

def operator
  @operator
end

#rightObject (readonly)

Returns the value of attribute right.



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

def right
  @right
end

Instance Method Details

#render(query) ⇒ String

Renders the comparison expression.

Parameters:

  • query (Cyrel::Query)

    The query object for rendering operands.

Returns:

  • (String)

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



49
50
51
52
53
54
55
56
57
# File 'lib/cyrel/expression/comparison.rb', line 49

def render(query)
  left_rendered = @left.render(query)
  if @right.nil? # Unary operator
    "(#{left_rendered} #{@cypher_operator})"
  else
    right_rendered = @right.render(query)
    "(#{left_rendered} #{@cypher_operator} #{right_rendered})"
  end
end