Sha256: bbcd1d2a43e7538ff857f6ba525562c49a3784c3a213c6d5a4cd8a7cef1416f7

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

class Redis
  class Mutex
    module Macro
      def self.included(base)
        base.extend ClassMethods
        base.class_eval do
          class << self
            attr_accessor :auto_mutex_methods
          end
          @auto_mutex_methods = {}
        end
      end

      module ClassMethods
        def auto_mutex(target, options={})
          self.auto_mutex_methods[target] = options
        end

        def method_added(target)
          return if target.to_s =~ /^auto_mutex_methods.*$/
          return unless self.auto_mutex_methods[target]
          without_method  = "#{target}_without_auto_mutex"
          with_method     = "#{target}_with_auto_mutex"
          return if method_defined?(without_method)

          define_method(with_method) do |*args|
            key = self.class.name << '#' << target.to_s
            options = self.class.auto_mutex_methods[target]

            Redis::Mutex.lock(key, options) do
              send(without_method, *args)
            end
          end

          alias_method without_method, target
          alias_method target, with_method
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis-mutex-1.2.0 lib/redis/mutex/macro.rb