method_cacher 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Method Cacher [![Build Status](https://secure.travis-ci.org/yaronw/method_cacher.png)](http://travis-ci.org/yaronw/method_cacher)
1
+ # Method Cacher
2
2
 
3
3
  Wraps specified methods with a mechanism that caches the return values.
4
4
 
@@ -43,8 +43,8 @@ MethodCacher.configure do
43
43
  end
44
44
  ```
45
45
 
46
- If used with Rails and no cache store is specified, method_cacher automatically uses the cache store configured with rails
47
- through a call to `Rails.cache`.
46
+ If used with Rails and no cache store is specified, method_cacher automatically uses the cache store configured in Rails
47
+ by calling `Rails.cache`.
48
48
 
49
49
  ## Usage
50
50
 
@@ -69,7 +69,8 @@ __Options:__
69
69
  + :singleton - Singleton methods to be cached are specified in an array of symbols passed through this option.
70
70
  + :obj\_key - A _proc_ that accepts the cached object as a single parameter. This _proc_ should return a value identifying this object.
71
71
  If this option is not specified, the object key defaults to the value returned by an instance method named _id_, which is convenient for usage
72
- with ActiveRecord objects.
72
+ with ActiveRecord objects. In cases when obj\_key evaluates to a nil, such as when using an ActiveRecord that hasn't been saved,
73
+ the cache is not used.
73
74
 
74
75
  `cache_method` can take any number of instance or singleton methods at once.
75
76
 
@@ -103,4 +104,18 @@ The clear cache methods take identical arguments as their respective original an
103
104
  clear the cache for the given set of arguments.
104
105
 
105
106
  So for example, issuing `clear_cache_for_foo('a')`, would clear the cache for a call to `foo('a')`
106
- but not to `foo('b')`.
107
+ but not to `foo('b')`.
108
+
109
+ # Examples
110
+
111
+ ## Caching ActiveRecord Methods That Return Relations
112
+
113
+ Consider the following ActiveRecord class:
114
+
115
+ ```ruby
116
+ class Factory < ActiveRecord::Base
117
+ has_many :widgets
118
+ end
119
+ ```
120
+
121
+ TODO: Continue here
@@ -152,6 +152,7 @@ module MethodCacher
152
152
  # Creates the key used to cache a method of the object.
153
153
  def cached_method_key(method_name, *args)
154
154
  obj_key = self.class.obj_key.call(self)
155
+ return nil unless obj_key # unable to form key and cache if obj_key evaluates to nil
155
156
  [self.class.name, obj_key, method_name, *args]
156
157
  end
157
158
  end
@@ -1,3 +1,3 @@
1
1
  module MethodCacher
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -193,6 +193,11 @@ describe MethodCacher::Base do
193
193
  obj2.foo.should_not == obj1.foo
194
194
  end
195
195
 
196
+ it "should not cache if :obj_key evaluates to nil" do
197
+ obj = NinthClass.new
198
+ lambda { obj.foo }.should_not be_twice_the_same
199
+ end
200
+
196
201
  it "should raise an exception if neither an :obj_key option is given nor the default fallback :id method is defined" do
197
202
  obj = SixthClass.new
198
203
  lambda { obj.foo }.should raise_error(NoMethodError)
@@ -261,4 +261,15 @@ class EighthClass
261
261
  end
262
262
 
263
263
  caches_method :foo2, :foo4
264
+ end
265
+
266
+
267
+ class NinthClass
268
+ include MethodCacher::Base
269
+
270
+ caches_method :foo, obj_key: proc { |obj| nil }
271
+
272
+ def foo
273
+ Dummy.function
274
+ end
264
275
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: method_cacher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-23 00:00:00.000000000Z
12
+ date: 2012-01-27 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &2152739300 !ruby/object:Gem::Requirement
16
+ requirement: &2165582100 !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: *2152739300
24
+ version_requirements: *2165582100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2152738880 !ruby/object:Gem::Requirement
27
+ requirement: &2165581680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2152738880
35
+ version_requirements: *2165581680
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: supermodel
38
- requirement: &2152738460 !ruby/object:Gem::Requirement
38
+ requirement: &2165581260 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2152738460
46
+ version_requirements: *2165581260
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: activesupport
49
- requirement: &2152738040 !ruby/object:Gem::Requirement
49
+ requirement: &2165580840 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2152738040
57
+ version_requirements: *2165580840
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: ruby-debug19
60
- requirement: &2152737620 !ruby/object:Gem::Requirement
60
+ requirement: &2165580420 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2152737620
68
+ version_requirements: *2165580420
69
69
  description: Easily cache instance and singleton (i.e. class) methods of ActiveRecord
70
70
  or any object.
71
71
  email: