Module: CouchbaseOrm::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/couchbase-orm/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#delete(with_cas: false, **options) ⇒ Object

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.



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/couchbase-orm/persistence.rb', line 100

def delete(with_cas: false, **options)
    options[:cas] = @__metadata__.cas if with_cas
    ::ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
        self.class.bucket.delete(@__metadata__.key, options)
    end

    @__metadata__.key = nil
    @id = nil

    clear_changes_information
    self.freeze
    self
end

#destroy(with_cas: false, **options) ⇒ Object Also known as: destroy!

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.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/couchbase-orm/persistence.rb', line 118

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
        ::ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
            self.class.bucket.delete(@__metadata__.key, options)
        end

        @__metadata__.key = nil
        @id = nil

        clear_changes_information
        freeze
    end
end

#destroyed?Boolean

Returns true if this object has been destroyed, otherwise returns false.

Returns:



62
63
64
# File 'lib/couchbase-orm/persistence.rb', line 62

def destroyed?
    !!(@__metadata__.cas && @__metadata__.key.nil?)
end

#new_record?Boolean Also known as: new?

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.

Returns:



56
57
58
# File 'lib/couchbase-orm/persistence.rb', line 56

def new_record?
    @__metadata__.cas.nil? && @__metadata__.key.nil?
end

#persisted?Boolean Also known as: exists?

Returns true if the record is persisted, i.e. it’s not a new record and it was not destroyed, otherwise returns false.

Returns:



68
69
70
71
# File 'lib/couchbase-orm/persistence.rb', line 68

def persisted?
    # Changed? is provided by ActiveModel::Dirty
    !!@__metadata__.key
end

#reloadObject

Reloads the record from the database.

This method finds record by its key and modifies the receiver in-place:



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/couchbase-orm/persistence.rb', line 195

def reload
    key = @__metadata__.key
    raise "unable to reload, model not persisted" unless key

    resp = nil
    ::ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
        resp = self.class.bucket.get(key, quiet: false, extended: true)
    end
    @__attributes__ = ::ActiveSupport::HashWithIndifferentAccess.new(resp.value)
    @__metadata__.key = resp.key
    @__metadata__.cas = resp.cas

    reset_associations
    clear_changes_information
    self
end

#save(**options) ⇒ Object

Saves the model.

If the model is new, a record gets created in the database, otherwise the existing record gets updated.



78
79
80
81
# File 'lib/couchbase-orm/persistence.rb', line 78

def save(**options)
    raise "Cannot save a destroyed document!" if destroyed?
    self.new_record? ? _create_record(**options) : _update_record(**options)
end

#save!(**options) ⇒ Object

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.



90
91
92
93
# File 'lib/couchbase-orm/persistence.rb', line 90

def save!(**options)
    self.class.fail_validate!(self) unless self.save(**options)
    self
end

#touch(**options) ⇒ Object

Updates the TTL of the document



213
214
215
216
217
218
219
220
# File 'lib/couchbase-orm/persistence.rb', line 213

def touch(**options)
    res = nil
    ::ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
        res = self.class.bucket.touch(@__metadata__.key, async: false, **options)
    end
    @__metadata__.cas = resp.cas
    self
end

#update(hash) ⇒ Object Also known as: update_attributes

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.



151
152
153
154
# File 'lib/couchbase-orm/persistence.rb', line 151

def update(hash)
    assign_attributes(hash)
    save
end

#update!(hash) ⇒ Object Also known as: update_attributes!

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.



159
160
161
162
# File 'lib/couchbase-orm/persistence.rb', line 159

def update!(hash)
    assign_attributes(hash) # Assign attributes is provided by ActiveModel::AttributeAssignment
    save!
end

#update_attribute(name, value) ⇒ Object

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.



144
145
146
147
# File 'lib/couchbase-orm/persistence.rb', line 144

def update_attribute(name, value)
    public_send(:"#{name}=", value)
    changed? ? save(validate: false) : true
end

#update_columns(with_cas: false, **hash) ⇒ Object

Updates the record without validating or running callbacks



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
# File 'lib/couchbase-orm/persistence.rb', line 166

def update_columns(with_cas: false, **hash)
    _id = @__metadata__.key
    raise "unable to update columns, model not persisted" unless _id

    assign_attributes(hash)

    # Ensure the type is set
    @__attributes__[:type] = self.class.design_document
    @__attributes__.delete(:id)

    options = {}
    options[:cas] = @__metadata__.cas if with_cas

    resp = nil
    ::ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
        resp = self.class.bucket.replace(_id, @__attributes__, **options)
    end

    # Ensure the model is up to date
    @__metadata__.key = resp.key
    @__metadata__.cas = resp.cas

    clear_changes_information
    self
end