lock_and_cache 2.2.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50f297c6ed5c5ab409f6d00f66a0c486c013c525
4
- data.tar.gz: 34806102910c3f13163b4d2aab2369924571405b
3
+ metadata.gz: 90b85cb8a2f2d566cbf6cd7a69375ee6772206c8
4
+ data.tar.gz: fae5041a751ce3617604de792c8f0cc6400919d2
5
5
  SHA512:
6
- metadata.gz: c5c6d160ac201fbc645ddd39d8e3c461da39a90a2ea8e9512705d508ad51c66accbca98deaa62432a08db1f11c6256db347015dea148f9bffff8d41ef63f2e72
7
- data.tar.gz: f84b2182fedcd9c7095a68a34dba3109b69ff301b818bf32d4eb77cad5f56ad6546f010141661011e5692e9a8ba46b15501d56d76eaace96fb88bbdd020812dc
6
+ metadata.gz: b54fa87634f6e9378eec8ab209eed851a41bd1e13e5cc10af293b80d0d68b217cd46ddbf496cbe49e959ec6f6dc100f582201e8ca316940eb0b6ad2db8b880c5
7
+ data.tar.gz: 1002dc77996ab481d8d150261cde2d7b8e9afc08640edad85ca5171ba7f6b5ac6d1dfcfbb249c956b67e13ef2813f48b88c04e69c07196cef84898e567800480
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ 3.0.0 / 2016-04-02
2
+
3
+ * Breaking changes
4
+
5
+ * In context mode (when you `include LockAndCache`), really call #lock_and_cache_key or #id on the instance
6
+
1
7
  2.2.2 / 2015-12-18
2
8
 
3
9
  * Bug fixes
data/README.md CHANGED
@@ -215,6 +215,7 @@ You can expire nil values with a different timeout (`nil_expires`) than other va
215
215
  ## Wishlist
216
216
 
217
217
  * Convert most tests to use standalone mode, which is easier to understand
218
+ * Make explicit Key unit tests
218
219
  * Check options
219
220
  * Lengthen heartbeat so it's not so sensitive
220
221
  * Clarify which options are seconds or milliseconds
@@ -17,21 +17,38 @@ module LockAndCache
17
17
  def extract_class_name(context)
18
18
  (context.class == ::Class) ? context.name : context.class.name
19
19
  end
20
+
21
+ # @private
22
+ #
23
+ # Extract id from context. Calls #lock_and_cache_key if available, otherwise #id
24
+ def extract_context_id(context)
25
+ if context.class == ::Class
26
+ nil
27
+ elsif context.respond_to?(:lock_and_cache_key)
28
+ context.lock_and_cache_key
29
+ elsif context.respond_to?(:id)
30
+ context.id
31
+ else
32
+ raise "#{context} must respond to #lock_and_cache_key or #id"
33
+ end
34
+ end
20
35
  end
21
36
 
22
37
  METHOD_NAME_IN_CALLER = /in `([^']+)'/
23
38
 
39
+ attr_reader :context
24
40
  attr_reader :method_id
25
- attr_reader :class_name
26
41
 
27
42
  def initialize(parts, options = {})
28
43
  @_parts = parts
44
+ @context = options[:context]
29
45
  @method_id = if options.has_key?(:method_id)
30
46
  options[:method_id]
31
47
  elsif options.has_key?(:caller)
32
48
  Key.extract_method_id_from_caller options[:caller]
49
+ elsif context
50
+ raise "supposed to call context with method_id or caller"
33
51
  end
34
- @class_name = Key.extract_class_name options[:context] if options.has_key?(:context)
35
52
  end
36
53
 
37
54
  # A (non-cryptographic) digest of the key parts for use as the cache key
@@ -46,8 +63,8 @@ module LockAndCache
46
63
 
47
64
  # A human-readable representation of the key parts
48
65
  def key
49
- @key ||= if method_id
50
- [class_name, method_id, parts]
66
+ @key ||= if context
67
+ [class_name, context_id, method_id, parts].compact
51
68
  else
52
69
  parts
53
70
  end
@@ -66,6 +83,14 @@ module LockAndCache
66
83
 
67
84
  alias debug key
68
85
 
86
+ def context_id
87
+ @context_id ||= Key.extract_context_id context
88
+ end
89
+
90
+ def class_name
91
+ @class_name ||= Key.extract_class_name context
92
+ end
93
+
69
94
  # An array of the parts we use for the key
70
95
  def parts
71
96
  @parts ||= @_parts.map do |v|
@@ -1,3 +1,3 @@
1
1
  module LockAndCache
2
- VERSION = '2.2.2'
2
+ VERSION = '3.0.0'
3
3
  end
@@ -17,6 +17,12 @@ class Foo
17
17
  end
18
18
  end
19
19
 
20
+ def cached_rand
21
+ lock_and_cache do
22
+ rand
23
+ end
24
+ end
25
+
20
26
  def click_null
21
27
  lock_and_cache do
22
28
  nil
@@ -48,6 +54,32 @@ class Foo
48
54
  end
49
55
  end
50
56
 
57
+ class FooId
58
+ include LockAndCache
59
+ def click
60
+ lock_and_cache do
61
+ nil
62
+ end
63
+ end
64
+ def id
65
+ @id ||= rand
66
+ end
67
+ end
68
+
69
+ class FooClass
70
+ class << self
71
+ include LockAndCache
72
+ def click
73
+ lock_and_cache do
74
+ nil
75
+ end
76
+ end
77
+ def id
78
+ raise "called id"
79
+ end
80
+ end
81
+ end
82
+
51
83
  require 'set'
52
84
  $clicking = Set.new
53
85
  class Bar
@@ -152,6 +184,34 @@ describe LockAndCache do
152
184
  expect(foo.click_last_hash_as_options).to eq(3)
153
185
  end
154
186
 
187
+ it "calls #lock_and_cache_key" do
188
+ expect(foo).to receive(:lock_and_cache_key)
189
+ foo.click
190
+ end
191
+
192
+ it "calls #lock_and_cache_key to differentiate" do
193
+ a = Foo.new 1
194
+ b = Foo.new 2
195
+ expect(a.cached_rand).not_to eq(b.cached_rand)
196
+ end
197
+ end
198
+
199
+ describe 'self-identification in context mode' do
200
+ it "calls #id for non-class" do
201
+ foo_id = FooId.new
202
+ expect(foo_id).to receive(:id)
203
+ foo_id.click
204
+ end
205
+ it "calls class name for non-class" do
206
+ foo_id = FooId.new
207
+ expect(FooId).to receive(:name)
208
+ foo_id.click
209
+ end
210
+ it "uses class name for class" do
211
+ expect(FooClass).to receive(:name)
212
+ expect(FooClass).not_to receive(:id)
213
+ FooClass.click
214
+ end
155
215
  end
156
216
 
157
217
  describe "locking" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lock_and_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.2
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seamus Abshere
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-18 00:00:00.000000000 Z
11
+ date: 2016-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport