Class: Cyrel::Clause::Where

Inherits:
Base
  • Object
show all
Defined in:
lib/cyrel/clause/where.rb

Overview

Represents a WHERE clause in a Cypher query.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*conditions) ⇒ Where

Returns a new instance of Where.

Parameters:



11
12
13
# File 'lib/cyrel/clause/where.rb', line 11

def initialize(*conditions)
  @conditions = conditions.flatten.map { |cond| Expression.coerce(cond) }
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



7
8
9
# File 'lib/cyrel/clause/where.rb', line 7

def conditions
  @conditions
end

Instance Method Details

#merge!(other_where) ⇒ Object

Merges conditions from another Where clause using AND.

Parameters:



36
37
38
39
# File 'lib/cyrel/clause/where.rb', line 36

def merge!(other_where)
  @conditions.concat(other_where.conditions)
  self
end

#render(query) ⇒ String?

Renders the WHERE clause. Combines multiple conditions using AND.

Parameters:

  • query (Cyrel::Query)

    The query object for rendering conditions.

Returns:

  • (String, nil)

    The Cypher string fragment, or nil if no conditions exist.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cyrel/clause/where.rb', line 19

def render(query)
  return nil if @conditions.empty?

  # Combine conditions with AND if there are multiple
  combined_condition = if @conditions.length == 1
                         @conditions.first
                       else
                         # Build a balanced AND tree for potentially better readability/performance?
                         # For now, simple left-associative AND is fine.
                         @conditions.reduce { |memo, cond| Expression::Logical.new(memo, :AND, cond) }
                       end

  "WHERE #{combined_condition.render(query)}"
end