-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersistence.rb
More file actions
293 lines (245 loc) · 11 KB
/
persistence.rb
File metadata and controls
293 lines (245 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# frozen_string_literal: true, encoding: ASCII-8BIT
require 'active_model'
require 'active_support/hash_with_indifferent_access'
require 'couchbase-orm/json_transcoder'
require 'couchbase-orm/encrypt'
module CouchbaseOrm
module Persistence
extend ActiveSupport::Concern
include CouchbaseOrm::Encrypt
included do
attribute :id, :string
end
module ClassMethods
def create(attributes = nil, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| create(attr, &block) }
else
instance = new(attributes, &block)
instance.save
instance.reset_object!
instance
end
end
def create!(attributes = nil, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| create!(attr, &block) }
else
instance = new(attributes, &block)
instance.save!
instance.reset_object!
instance
end
end
# Raise an error if validation failed.
def fail_validate!(document)
raise Error::RecordInvalid.new("Failed to save the record", document)
end
# Allow classes to overwrite the default document name
# extend ActiveModel::Naming (included by ActiveModel::Model)
def design_document(name = nil)
return @design_document unless name
@design_document = name.to_s
end
# Set a default design document
def inherited(child)
super
child.instance_eval do
@design_document = child.name.underscore
end
end
end
# Returns true if this object hasn't been saved yet -- that is, a record
# for the object doesn't exist in the database yet; otherwise, returns false.
def new_record?
@__metadata__.cas.nil?
end
alias_method :new?, :new_record?
# Returns true if this object has been destroyed, otherwise returns false.
def destroyed?
@destroyed ||= false
end
# Returns true if the record is persisted, i.e. it's not a new record and it was
# not destroyed, otherwise returns false.
def persisted?
!new_record? && !destroyed?
end
alias_method :exists?, :persisted?
# Saves the model.
#
# If the model is new, a record gets created in the database, otherwise
# the existing record gets updated.
def save(**options)
raise "Cannot save a destroyed document!" if destroyed?
self.new_record? ? _create_record(**options) : _update_record(**options)
end
# Saves the model.
#
# If the model is new, a record gets created in the database, otherwise
# the existing record gets updated.
#
# By default, #save! always runs validations. If any of them fail
# CouchbaseOrm::Error::RecordInvalid gets raised, and the record won't be saved.
def save!(**options)
self.class.fail_validate!(self) unless self.save(**options)
self
end
# Deletes the record in the database and freezes this instance to
# reflect that no changes should be made (since they can't be
# persisted). Returns the frozen instance.
#
# The record is simply removed, no callbacks are executed.
def delete(with_cas: false, **options)
options[:cas] = @__metadata__.cas if with_cas
CouchbaseOrm.logger.debug "Data - Delete #{self.id}"
self.class.collection.remove(self.id, **options)
self.id = nil
clear_changes_information
@destroyed = true
self.freeze
self
end
alias :remove :delete
# Deletes the record in the database and freezes this instance to reflect
# that no changes should be made (since they can't be persisted).
#
# There's a series of callbacks associated with #destroy.
def destroy(with_cas: false, **options)
return self if destroyed?
raise 'model not persisted' unless persisted?
run_callbacks :destroy do
destroy_associations!
options[:cas] = @__metadata__.cas if with_cas
CouchbaseOrm.logger.debug "Data - Destroy #{id}"
self.class.collection.remove(id, **options)
self.id = nil
clear_changes_information
@destroyed = true
freeze
end
end
alias_method :destroy!, :destroy
# Updates a single attribute and saves the record.
# This is especially useful for boolean flags on existing records. Also note that
#
# * Validation is skipped.
# * \Callbacks are invoked.
def update_attribute(name, value)
public_send(:"#{name}=", value)
changed? ? save(validate: false) : true
end
def assign_attributes(hash)
hash = hash.with_indifferent_access if hash.is_a?(Hash)
# Filter unknown attributes if raise_on_unknown_attributes is false
if !self.class.raise_on_unknown_attributes
known_attrs = hash.slice(*self.class.attribute_names).except("type")
unknown_attrs = hash.keys - self.class.attribute_names - ["type"]
if unknown_attrs.any?
CouchbaseOrm.logger.warn "Ignoring unknown attribute(s) for #{self.class.name}: #{unknown_attrs.join(', ')}"
end
super(known_attrs)
else
super(hash.except("type"))
end
end
# Updates the attributes of the model from the passed-in hash and saves the
# record. If the object is invalid, the saving will fail and false will be returned.
def update(hash)
assign_attributes(hash)
save
end
alias_method :update_attributes, :update
# Updates its receiver just like #update but calls #save! instead
# of +save+, so an exception is raised if the record is invalid and saving will fail.
def update!(hash)
assign_attributes(hash) # Assign attributes is provided by ActiveModel::AttributeAssignment
save!
end
alias_method :update_attributes!, :update!
# Updates the record without validating or running callbacks.
# Updates only the attributes that are passed in as parameters
# except if there is more than 16 attributes, in which case
# the whole record is saved.
def update_columns(with_cas: false, **hash)
raise "unable to update columns, model not persisted" unless id
assign_attributes(hash)
options = {extended: true}
options[:cas] = @__metadata__.cas if with_cas
# There is a limit of 16 subdoc operations per request
resp = if hash.length <= 16
self.class.collection.mutate_in(
id,
hash.map { |k, v| Couchbase::MutateInSpec.replace(k.to_s, v) }
)
else
# Fallback to writing the whole document
CouchbaseOrm.logger.debug { "Data - Replace #{id} #{attributes.to_s.truncate(200)}" }
self.class.collection.replace(id, attributes.except("id").merge(type: self.class.design_document), **options)
end
# Ensure the model is up to date
@__metadata__.cas = resp.cas
changes_applied
self
end
# Reloads the record from the database.
#
# This method finds record by its key and modifies the receiver in-place:
def reload
raise "unable to reload, model not persisted" unless id
CouchbaseOrm.logger.debug "Data - Get #{id}"
resp = self.class.collection.get!(id)
assign_attributes(decode_encrypted_attributes(resp.content.except("id", *self.class.ignored_properties ))) # API return a nil id
@__metadata__.cas = resp.cas
reset_associations
clear_changes_information
reset_object!
self
end
# Updates the TTL of the document
def touch(**options)
CouchbaseOrm.logger.debug "Data - Touch #{id}"
_res = self.class.collection.touch(id, async: false, **options)
@__metadata__.cas = resp.cas
self
end
def _update_record(*_args, with_cas: false, **options)
return false unless perform_validations(:update, options)
return true unless changed? || self.class.attribute_types.any? { |_, type| type.is_a?(CouchbaseOrm::Types::Nested) || type.is_a?(CouchbaseOrm::Types::Array) }
run_callbacks :update do
run_callbacks :save do
options[:cas] = @__metadata__.cas if with_cas
CouchbaseOrm.logger.debug { "_update_record - replace #{id} #{serialized_attributes.to_s.truncate(200)}" }
if options[:transcoder].nil?
options[:transcoder] = CouchbaseOrm::JsonTranscoder.new(json_validation_config: self.class.json_validation_config)
end
resp = self.class.collection.replace(id, serialized_attributes.except("id").merge(type: self.class.design_document), Couchbase::Options::Replace.new(**options))
# Ensure the model is up to date
@__metadata__.cas = resp.cas
changes_applied
true
end
end
end
def _create_record(*_args, **options)
return false unless perform_validations(:create, options)
run_callbacks :create do
run_callbacks :save do
assign_attributes(id: self.class.uuid_generator.next(self)) unless self.id
CouchbaseOrm.logger.debug { "_create_record - Upsert #{id} #{serialized_attributes.to_s.truncate(200)}" }
if options[:transcoder].nil?
options[:transcoder] = CouchbaseOrm::JsonTranscoder.new(json_validation_config: self.class.json_validation_config)
end
resp = self.class.collection.upsert(self.id, serialized_attributes.except("id").merge(type: self.class.design_document), Couchbase::Options::Upsert.new(**options))
# Ensure the model is up to date
@__metadata__.cas = resp.cas
changes_applied
true
end
end
end
def perform_validations(context, options = {})
return valid?(context) if options[:validate] != false
true
end
end
end