Sha256: 8399e0eb9eda3a209cdfc321fba6369675af4a4fadfa1103ec71d18e2d7f5500

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'active_record'
require 'active_record/database_mutex/version'
require 'active_record/database_mutex/implementation'

module ActiveRecord
  # This module is mixed into ActiveRecord::Base to provide the mutex methods
  # that return a mutex for a particular ActiveRecord::Base subclass/instance.
  module DatabaseMutex
    # This is the base exception of all mutex exceptions.
    class MutexError < ActiveRecordError; end

    # This exception is raised if a mutex of the given name isn't locked at the
    # moment and unlock was called.
    class MutexUnlockFailed < MutexError; end

    # This exception is raised if a mutex of the given name is locked at the
    # moment and lock was called again.
    class MutexLocked < MutexError; end

    class MutexInvalidState <  MutexError; end

    def self.included(modul)
      modul.instance_eval do
        extend ClassMethods
      end
    end

    # XXX
    def self.for(name)
      Implementation.new(:name => name)
    end

    module ClassMethods
      # Returns a mutex instance for this ActiveRecord subclass.
      def mutex
        @mutex ||= Implementation.new(:name => name)
      end
    end

    # Returns a mutex instance for this ActiveRecord instance.
    def mutex
      if persisted?
        @mutex ||= Implementation.new(:name => "#{id}@#{self.class.name}")
      else
        raise MutexInvalidState, "instance #{inspect} not persisted"
      end
    end
  end
end

ActiveRecord::Base.class_eval do
  include ActiveRecord::DatabaseMutex
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_mutex-2.0.0 lib/active_record/database_mutex.rb