lib/og/mixin/optimistic_locking.rb in og-0.20.0 vs lib/og/mixin/optimistic_locking.rb in og-0.21.0

- old
+ new

@@ -14,45 +14,45 @@ # # 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 + property :lock_version, Fixnum, :default => 0 + pre "@lock_version = 0", :on => :og_insert - def self.append_features(base) #:nodoc: - PropertyUtils.copy_features(self, base) + def self.append_features(base) #:nodoc: + PropertyUtils.copy_features(self, base) - super + super - 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 - end - - def update_with_lock - lock = @lock_version - @lock_version += 1 + 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 + 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 + 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 + 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 - + unless save_without_lock(:condition => "lock_version=#{lock}") == 1 + raise(StaleObjectError, 'Attempted to update a stale object') + end + end + end end # * George Moschovitis <gm@navel.gr>