morlock 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  class Morlock
2
- class UnknownGemClient < StandardError; end
2
+ class UnknownGemClient < StandardError;
3
+ end
3
4
 
4
5
  class GemClient
5
6
  GEM_CLIENTS = []
@@ -9,6 +10,8 @@ class Morlock
9
10
  end
10
11
 
11
12
  def self.wrap(client)
13
+ return TestGemClient.new(nil) if client == :test
14
+
12
15
  GEM_CLIENTS.each do |gem, gem_client|
13
16
  if (eval(gem) rescue false) && client.is_a?(eval(gem))
14
17
  return gem_client.new(client)
@@ -18,14 +21,24 @@ class Morlock
18
21
  raise UnknownGemClient.new("You provided Morlock a memcached client of an unknown type: #{client.class}")
19
22
  end
20
23
 
21
- def delete(key)
22
- @client.delete(key)
24
+
25
+ def no_server_error(e)
26
+ STDERR.puts "WARNING: No memcached server found; Memlock was unable to create a lock. (#{e.message})"
27
+ true
23
28
  end
24
29
  end
25
30
 
26
31
  class DalliGemClient < GemClient
27
32
  def add(key, expiration)
28
33
  @client.add(key, 1, expiration)
34
+ rescue => e
35
+ no_server_error e
36
+ end
37
+
38
+ def delete(key)
39
+ @client.delete(key)
40
+ rescue => e
41
+ true
29
42
  end
30
43
  end
31
44
  GemClient::GEM_CLIENTS << ["Dalli::Client", DalliGemClient]
@@ -33,7 +46,26 @@ class Morlock
33
46
  class MemcacheGemClient < GemClient
34
47
  def add(key, expiration)
35
48
  @client.add(key, 1, expiration, true) !~ /NOT_STORED/
49
+ rescue MemCache::MemCacheError => e
50
+ no_server_error e
51
+ end
52
+
53
+ def delete(key)
54
+ @client.delete(key)
55
+ rescue MemCache::MemCacheError => e
56
+ true
36
57
  end
37
58
  end
38
59
  GemClient::GEM_CLIENTS << ["MemCache", MemcacheGemClient]
60
+
61
+
62
+ class TestGemClient < GemClient
63
+ def add(key, expiration)
64
+ true
65
+ end
66
+
67
+ def delete(key)
68
+ true
69
+ end
70
+ end
39
71
  end
data/lib/morlock/rails.rb CHANGED
@@ -19,11 +19,23 @@ class Morlock
19
19
  self.setup_for_mem_cache_store
20
20
  end
21
21
 
22
+ def self.setup_for_test_store
23
+ Rails.module_eval do
24
+ class << self
25
+ def morlock
26
+ @@morlock ||= Morlock.new(:test)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
22
32
  def self.detect_memcache_gem
23
33
  if defined?(ActiveSupport::Cache::MemCacheStore) && Rails.cache.is_a?(ActiveSupport::Cache::MemCacheStore)
24
34
  setup_for_mem_cache_store
25
35
  elsif defined?(ActiveSupport::Cache::DalliStore) && Rails.cache.is_a?(ActiveSupport::Cache::DalliStore)
26
36
  setup_for_dalli_store
37
+ elsif Rails.env.test?
38
+ setup_for_test_store
27
39
  else
28
40
  Rails.logger.warn "WARNING: Morlock detected that you are not using the Rails ActiveSupport::Cache::MemCacheStore. Rails.morlock will not be setup."
29
41
  end
@@ -1,3 +1,3 @@
1
1
  class Morlock
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/spec/morlock_spec.rb CHANGED
@@ -57,6 +57,26 @@ describe Morlock do
57
57
  morlock.lock("some_key", :expiration => 60).should == false
58
58
  end
59
59
  end
60
+
61
+ describe "with test" do
62
+ before do
63
+ @mock_client = :test
64
+ @morlock = Morlock.new(@mock_client)
65
+ end
66
+
67
+ it "should make a test client" do
68
+ @morlock.client.is_a?(Morlock::TestGemClient).should == true
69
+ end
70
+
71
+ it "should yield and return true when trying to lock" do
72
+ yielded = false
73
+ result = @morlock.lock("some_key") do
74
+ yielded = true
75
+ end
76
+ yielded.should be_true
77
+ result.should be_true
78
+ end
79
+ end
60
80
  end
61
81
 
62
82
  context "general behavior" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: morlock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70342324414200 !ruby/object:Gem::Requirement
16
+ requirement: &70284346550040 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70342324414200
24
+ version_requirements: *70284346550040
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rr
27
- requirement: &70342324413700 !ruby/object:Gem::Requirement
27
+ requirement: &70284346549520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70342324413700
35
+ version_requirements: *70284346549520
36
36
  description: ''
37
37
  email:
38
38
  - pair+andrew+chris@mavenlink.com