module WithLockVersion::ActiveRecordExt::Core

Public Class Methods

included(model) click to toggle source
# File lib/with_lock_version/active_record_ext.rb, line 14
def self.included(model)
  model.extend ClassMethods
end

Public Instance Methods

with_lock_version(target_lock_version) { || ... } click to toggle source
# File lib/with_lock_version/active_record_ext.rb, line 18
def with_lock_version(target_lock_version)
  unless lock_version.to_i == target_lock_version.to_i
    raise_error
  end

  with_lock do
    unless lock_version.to_i == target_lock_version.to_i
      raise_error
    end

    touch # Update lock_version + 1

    yield
  end
end

Private Instance Methods

raise_error() click to toggle source
# File lib/with_lock_version/active_record_ext.rb, line 36
def raise_error
  if ActiveRecord::StaleObjectError.method(:new).arity == 0
    raise ActiveRecord::StaleObjectError
  else
    raise ActiveRecord::StaleObjectError.new(self, "with_lock_version mismatch")
  end
end