Class: Cyrel::Expression::Comparison
- 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
-
#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) ⇒ Comparison
constructor
A new instance of Comparison.
-
#render(query) ⇒ String
Renders the comparison expression.
Methods inherited from Base
#!=, #%, #&, #*, #+, #-, #/, #<, #<=, #==, #=~, #>, #>=, #^, #|
Constructor Details
#initialize(left, operator, right = nil) ⇒ Comparison
Returns a new instance of Comparison.
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
#left ⇒ Object (readonly)
Returns the value of attribute left.
7 8 9 |
# File 'lib/cyrel/expression/comparison.rb', line 7 def left @left end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
7 8 9 |
# File 'lib/cyrel/expression/comparison.rb', line 7 def operator @operator end |
#right ⇒ Object (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.
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 |