Module: Lockless::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/lockless/model.rb

Overview

Handles lockless saves

Instance Method Summary collapse

Instance Method Details

#lockless_saveBoolean

Saves record if it has not be modified in the time after it was loaded from the database.

false is returned if record is outdated or invalid

Returns:

  • (Boolean)

    Similar to .save, true is returned if the model is updated



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lockless/model.rb', line 19

def lockless_save
  return false unless valid?
  old_lockless_uuid = lockless_uuid
  return save if new_record?

  run_callbacks(:save) do |variable|
    new_attrs = changed.collect { |prop| [prop.to_sym, self[prop]] }.to_h

    update_count = self.class.where(id: id, lockless_uuid: old_lockless_uuid).update_all(new_attrs)
    if update_count == 1
      changes_applied
      true
    else
      self.lockless_uuid = old_lockless_uuid
      false
    end
  end
end

#lockless_save!Boolean

Saves record if it has not be modified in the time after it was loaded from the database.

false is returned if record is outdated

Returns:

  • (Boolean)

    Similar to .save!, true is returned if the model is updated

Raises:

  • (ActiveRecord::RecordInvalid)

    if record is invalid



44
45
46
47
# File 'lib/lockless/model.rb', line 44

def lockless_save!
  validate!
  lockless_save
end