Module: LockMethod::ClassMethods

Defined in:
lib/lock_method.rb

Overview

All Classes (but not instances), get the .lock_method method.

Instance Method Summary collapse

Instance Method Details

#lock_method(method_id, ttl = nil) ⇒ Object

Lock a method. TTL in seconds, defaults to whatever’s in LockMethod.config.default_ttl

Note 2: Check out LockMethod.config.default_ttl… the default is 24 hours!

Example:

class Blog
  # [...]
  def get_latest_entries
    sleep 5
  end
  # [...]
  lock_method :get_latest_entries
  # if you wanted a different ttl...
  # lock_method :get_latest_entries, 800 #seconds
end


45
46
47
48
49
50
51
52
# File 'lib/lock_method.rb', line 45

def lock_method(method_id, ttl = nil)
  original_method_id = "_unlocked_#{method_id}"
  alias_method original_method_id, method_id
  define_method method_id do |*args|
    lock = ::LockMethod::Lock.new :obj => self, :method_id => method_id, :original_method_id => original_method_id, :args => args, :ttl => ttl
    lock.call_original_method
  end
end