Class: Cyrel::Clause::Set
Overview
Represents a SET clause in a Cypher query. Used for setting properties or labels on nodes/relationships.
Instance Attribute Summary collapse
-
#assignments ⇒ Object
readonly
Returns the value of attribute assignments.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#initialize(assignments) ⇒ Set
constructor
Initializes a SET clause.
-
#merge!(other_set) ⇒ Object
Merges assignments from another Set clause.
-
#render(query) ⇒ String?
Renders the SET clause.
Constructor Details
#initialize(assignments) ⇒ Set
Initializes a SET clause.
19 20 21 |
# File 'lib/cyrel/clause/set.rb', line 19 def initialize(assignments) @assignments = self.class.normalize_assignments(assignments) end |
Instance Attribute Details
#assignments ⇒ Object (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.
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.
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.
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 |