Sha256: 93353d07454b0dfe6e8c48d78dfc3174c2a8f47e07c483baefd8993dbf98ef9b

Contents?: true

Size: 1.66 KB

Versions: 12

Compression:

Stored size: 1.66 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

    class MutexSystemError < MutexError; end

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

    # Return a mutex implementation for the mutex named +name+.
    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, defined?(Rails) ? Rails.env : ENV['RAILS_ENV'] ].compact * ?@
        )
      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

12 entries across 12 versions & 1 rubygems

Version Path
active_record_mutex-2.5.1 lib/active_record/database_mutex.rb
active_record_mutex-2.5.0 lib/active_record/database_mutex.rb
active_record_mutex-2.4.0 lib/active_record/database_mutex.rb
active_record_mutex-2.3.8 lib/active_record/database_mutex.rb
active_record_mutex-2.3.7 lib/active_record/database_mutex.rb
active_record_mutex-2.3.6 lib/active_record/database_mutex.rb
active_record_mutex-2.3.5 lib/active_record/database_mutex.rb
active_record_mutex-2.3.4 lib/active_record/database_mutex.rb
active_record_mutex-2.3.3 lib/active_record/database_mutex.rb
active_record_mutex-2.3.2 lib/active_record/database_mutex.rb
active_record_mutex-2.3.1 lib/active_record/database_mutex.rb
active_record_mutex-2.3.0 lib/active_record/database_mutex.rb