Skip to content

Commit 551d89c

Browse files
Refactor: Move assign_attributes to Document class for consistency
- Move assign_attributes override from Persistence to Document class - Ensures consistent behavior for both Document and NestedDocument - Use Set for efficient unknown attribute lookup (performance optimization) - Add require 'set' to base.rb - Remove duplicate implementation from persistence.rb This addresses code review feedback about consistency and performance.
1 parent da85db9 commit 551d89c

2 files changed

Lines changed: 26 additions & 15 deletions

File tree

lib/couchbase-orm/base.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true, encoding: ASCII-8BIT
22

33

4+
require 'set'
45
require 'active_model'
56
require 'active_support/hash_with_indifferent_access'
67
require 'couchbase'
@@ -115,6 +116,29 @@ def attribute_writer_missing(name, value)
115116
end
116117
end
117118

119+
# Override assign_attributes to filter unknown attributes when raise_on_unknown_attributes is false
120+
# This ensures consistent behavior across Document and NestedDocument
121+
def assign_attributes(hash)
122+
hash = hash.with_indifferent_access if hash.is_a?(Hash)
123+
124+
if self.class.raise_on_unknown_attributes
125+
super(hash.except("type"))
126+
else
127+
# Filter unknown attributes
128+
known_names = self.class.attribute_names
129+
known_attrs = hash.slice(*known_names)
130+
131+
# Use a Set for efficient lookup of unknown keys
132+
known_names_set = known_names.to_set
133+
unknown_keys = hash.keys.reject { |k| known_names_set.include?(k) || k == "type" }
134+
135+
if unknown_keys.any?
136+
CouchbaseOrm.logger.warn "Ignoring unknown attribute(s) for #{self.class.name}: #{unknown_keys.join(', ')}"
137+
end
138+
super(known_attrs)
139+
end
140+
end
141+
118142
protected
119143

120144
def serialized_attributes

lib/couchbase-orm/persistence.rb

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,8 @@ def update_attribute(name, value)
153153
changed? ? save(validate: false) : true
154154
end
155155

156-
def assign_attributes(hash)
157-
hash = hash.with_indifferent_access if hash.is_a?(Hash)
158-
159-
# Filter unknown attributes if raise_on_unknown_attributes is false
160-
if !self.class.raise_on_unknown_attributes
161-
known_attrs = hash.slice(*self.class.attribute_names).except("type")
162-
unknown_attrs = hash.keys - self.class.attribute_names - ["type"]
163-
if unknown_attrs.any?
164-
CouchbaseOrm.logger.warn "Ignoring unknown attribute(s) for #{self.class.name}: #{unknown_attrs.join(', ')}"
165-
end
166-
super(known_attrs)
167-
else
168-
super(hash.except("type"))
169-
end
170-
end
156+
# Note: assign_attributes is now handled in Document class (base.rb)
157+
# to ensure consistent behavior across Document and NestedDocument
171158

172159
# Updates the attributes of the model from the passed-in hash and saves the
173160
# record. If the object is invalid, the saving will fail and false will be returned.

0 commit comments

Comments
 (0)