Class: Cyrel::Clause::Set

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

Overview

Represents a SET clause in a Cypher query. Used for setting properties or labels on nodes/relationships.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(assignments) ⇒ Set

Initializes a SET clause.

Parameters:

  • assignments (Hash, Array)
    • Hash: { variable_or_prop_access => value_expression, … } e.g., { Cyrel.prop(:n, :name) => “New Name”, Cyrel.prop(:r, :weight) => 10 } e.g., { n: { name: “New Name”, age: 30 } } # For SET n = properties or n = properties e.g., { Cyrel.plus(:n) => { name: “New Name” } } # For SET n = { name: … }

    • Array: [[variable, label_string], …] # For SET n:Label e.g., [[:n, “NewLabel”], [:m, “AnotherLabel”]]

    Note: Mixing hash and array styles in one call is not directly supported, use multiple SET clauses if needed.



19
20
21
# File 'lib/cyrel/clause/set.rb', line 19

def initialize(assignments)
  @assignments = self.class.normalize_assignments(assignments)
end

Instance Attribute Details

#assignmentsObject (readonly)

Returns the value of attribute assignments.



8
9
10
# File 'lib/cyrel/clause/set.rb', line 8

def assignments
  @assignments
end

Class Method Details

.normalize_assignments(assignments) ⇒ Array<Array>

Normalize raw SET assignments (Hash of props/labels or Array of label pairs) into the internal tuple form consumed by both Clause::Set and Query#set.

Parameters:

  • assignments (Hash, Array)

Returns:

  • (Array<Array>)

    tuples like [:property, …], [:variable_properties, …], [:label, …]



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cyrel/clause/set.rb', line 27

def self.normalize_assignments(assignments)
  case assignments
  when Hash
    assignments.flat_map do |key, value|
      case key
      when Expression::PropertyAccess
        # SET n.prop = value
        [[:property, key, Expression.coerce(value)]]
      when Symbol, String
        # SET n = properties
        raise ArgumentError, 'Value for variable assignment must be a Hash (for SET n = {props})' unless value.is_a?(Hash)

        [[:variable_properties, key.to_sym, Expression.coerce(value), :assign]]
      when Cyrel::Plus
        # SET n += properties
        raise ArgumentError, 'Value for variable assignment must be a Hash (for SET n += {props})' unless value.is_a?(Hash)

        [[:variable_properties, key.variable.to_sym, Expression.coerce(value), :merge]]
      else
        raise ArgumentError, "Invalid key type in SET assignments hash: #{key.class}"
      end
    end
  when Array
    assignments.map do |item|
      unless item.is_a?(Array) && item.length == 2
        raise ArgumentError,
              "Invalid label assignment format. Expected [[:variable, 'Label'], ...], got #{item.inspect}"
      end

      # SET n:Label
      [:label, item[0].to_sym, item[1]]
    end
  else
    raise ArgumentError, "Invalid assignments type for SET clause: #{assignments.class}"
  end
end

Instance Method Details

#merge!(other_set) ⇒ Object

Merges assignments from another Set clause.

Parameters:



79
80
81
82
83
84
# File 'lib/cyrel/clause/set.rb', line 79

def merge!(other_set)
  # Simple concatenation, assumes no conflicting assignments on the same property.
  # More sophisticated merging might be needed depending on requirements.
  @assignments.concat(other_set.assignments)
  self
end

#render(query) ⇒ String?

Renders the SET clause.

Parameters:

  • query (Cyrel::Query)

    The query object for rendering expressions.

Returns:

  • (String, nil)

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



67
68
69
70
71
72
73
74
75
# File 'lib/cyrel/clause/set.rb', line 67

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

  set_parts = @assignments.map do |assignment|
    render_assignment(assignment, query)
  end

  "SET #{set_parts.join(', ')}"
end