Class: Morlock

Inherits:
Object
  • Object
show all
Defined in:
lib/morlock/base.rb,
lib/morlock/rails.rb,
lib/morlock/version.rb,
lib/morlock/gem_client.rb

Defined Under Namespace

Classes: DalliGemClient, GemClient, MemcacheGemClient, MorlockRailtie, TestGemClient, UnknownGemClient

Constant Summary collapse

DEFAULT_EXPIRATION =
60
VERSION =
"0.0.7"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Morlock

Returns a new instance of Morlock.



8
9
10
# File 'lib/morlock/base.rb', line 8

def initialize(client)
  @client = Morlock::GemClient.wrap(client)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/morlock/base.rb', line 6

def client
  @client
end

Instance Method Details

#lock(key, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/morlock/base.rb', line 12

def lock(key, options = {})
  lock_obtained = @client.add(key, options[:expiration] || DEFAULT_EXPIRATION)
  puts "Lock for #{key} #{lock_obtained ? "obtained" : "not obtained"}." if options[:verbose]
  yield if lock_obtained && block_given?
  options[:success].call if lock_obtained && options[:success]
  options[:failure].call if !lock_obtained && options[:failure]
  lock_obtained
ensure
  if lock_obtained
    if @client.delete(key)
      puts "Lock removed for #{key}" if options[:verbose]
    else
      puts "Someone else removed the lock for #{key}!" if options[:verbose]
    end
  end
end