attr_cached 1.2 → 1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/attr_cached.rb +10 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1bf28ed293f2b11115936be37caabc5b325247bd
|
4
|
+
data.tar.gz: 4069cc845f60b0802b110e100e7b9dccf2001aa7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b4f4500b830da85c163ec59f2b09bfc91b6ca19d39dab6cf7212781a0807a519fcbc9902ca822fc9ab578f0b21e80da22f381137d45742383f79a3c91755843
|
7
|
+
data.tar.gz: aa572f08f82e88f561b7d4b054065cfe8cd09914e548b8be3a5393e234306a4c1882ecb2908ae91b64e90fb521f1f325e35bdc49d1bb9da5f9b15f1be86a6364
|
data/lib/attr_cached.rb
CHANGED
@@ -3,13 +3,13 @@ class ActiveRecord::Base
|
|
3
3
|
# The attr_cached method should be called in the body of an
|
4
4
|
# ActiveRecord model which has an 'id' attribute.
|
5
5
|
def self.attr_cached *attributes
|
6
|
+
# Assign a reusable message to raise upon the occurence of a nil id.
|
7
|
+
missing_id = 'The \'id\' cannot be nil when calling an attribute created by attr_memcached'
|
8
|
+
|
6
9
|
# Iterate through each attribute
|
7
10
|
attributes.each do |attribute|
|
8
11
|
# Set the beginning to the key to be used in both the read and write methods.
|
9
12
|
key = "#{name.downcase}_#{attribute}_"
|
10
|
-
|
11
|
-
# Assign a reusable message to raise upon the occurence of a nil id.
|
12
|
-
missing_id = 'The \'id\' cannot be nil when calling an attribute created by attr_memcached'
|
13
13
|
|
14
14
|
# Define the method to read from the cache
|
15
15
|
define_method attribute do
|
@@ -29,6 +29,13 @@ class ActiveRecord::Base
|
|
29
29
|
Rails.cache.delete(key + id.to_s)
|
30
30
|
end
|
31
31
|
end
|
32
|
+
class_name = "#{name.downcase}_"
|
33
|
+
define_method :delete_cache do
|
34
|
+
raise missing_id if id.nil?
|
35
|
+
attributes.each do |attribute|
|
36
|
+
Rails.cache.delete class_name + "#{attribute}_#{id}"
|
37
|
+
end
|
38
|
+
end
|
32
39
|
end
|
33
40
|
|
34
41
|
end
|