Sha256: 901902607c5c637f4d6e5ff6b8c4626f327e9a26541170e81cef5042528b11b2

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

require 'facets/core/module/on_included'

module Glue

# This error is thrown when you the object you are trynig
# to update is allready updated by another thread.

class StaleObjectError < StandardError
end

# Include this module into entity classes to provide optimistic
# locking suport. For more information on optimistic locking
# please consult:
#
# http://c2.com/cgi/wiki?OptimisticLocking
# http://en.wikipedia.org/wiki/Optimistic_concurrency_control

module Locking
  property :lock_version, Fixnum, :default => 0
  pre "@lock_version = 0", :on => :og_insert
  
  on_included %{
    base.module_eval do
      def self.enchant
        self.send :alias_method, :update_without_lock, :update
        self.send :alias_method, :update, :update_with_lock
        self.send :alias_method, :save_without_lock, :save
        self.send :alias_method, :save, :save_with_lock
      end
    end
  }
  
  def update_with_lock
    lock = @lock_version
    @lock_version += 1

    unless update_without_lock(:condition => "lock_version=#{lock}") == 1  
      raise(StaleObjectError, 'Attempted to update a stale object')
    end
  end

  def save_with_lock
    lock = @lock_version
    @lock_version += 1

    unless save_without_lock(:condition => "lock_version=#{lock}") == 1  
      raise(StaleObjectError, 'Attempted to update a stale object')
    end
  end
  
end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
og-0.41.0 lib/glue/optimistic_locking.rb
og-0.40.0 lib/glue/optimistic_locking.rb