has_cache 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/has_cache/cache.rb +27 -15
- data/lib/has_cache/mixin.rb +1 -1
- data/lib/has_cache/version.rb +1 -1
- 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: f826a005438e6e2239208a8dd6e7e320a8f00d12
|
4
|
+
data.tar.gz: 257aabcfca729a031f47cd32dfa598c43fa12a52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad0e0a76f1797361e988e6b4601da3690c21dd157550237c022c0d05528ec98c3847c90f5fc4e6651857af89601e1272afa968a86e2a4e583f54281bb1e59e0d
|
7
|
+
data.tar.gz: f2d655273163d28ef458592eade0cc6d047e5f1adb91ae6efcd5243a0e012645b6f1a8243f67e382c4d797d2e1d4a6a3e0be801977eeccfba72f9268185188bf
|
data/lib/has_cache/cache.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'sourcify'
|
2
2
|
|
3
3
|
module HasCache
|
4
|
-
# The cache class proxies calls to the original cache_target, caching the
|
5
|
-
# and returns the cached results if passed the same cache_target
|
4
|
+
# The cache class proxies calls to the original cache_target, caching the
|
5
|
+
# results, and returns the cached results if passed the same cache_target
|
6
|
+
# and arguments
|
6
7
|
class Cache
|
7
8
|
extend HasCache::Utilities
|
8
9
|
include HasCache::Utilities
|
@@ -32,11 +33,10 @@ module HasCache
|
|
32
33
|
else
|
33
34
|
Rails.cache.fetch(key, o.cache_options) do
|
34
35
|
if o.cache_target.is_a?(Class)
|
35
|
-
|
36
|
+
extract_result(o.cache_target.class_eval(&block))
|
36
37
|
else
|
37
|
-
|
38
|
+
extract_result(o.cache_target.instance_eval(&block))
|
38
39
|
end
|
39
|
-
extract_result(result)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
end
|
@@ -49,18 +49,30 @@ module HasCache
|
|
49
49
|
@cache_root = [cache_target.name, :class]
|
50
50
|
else
|
51
51
|
@cache_root = [cache_target.class.name, :instance]
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
52
|
+
|
53
|
+
return if cache_target.respond_to?(:has_cache_key)
|
54
|
+
|
55
|
+
if cache_target.class.respond_to?(:primary_key)
|
56
|
+
primary_key = cache_target.class.send(:primary_key)
|
57
|
+
|
58
|
+
# Support composite primary keys
|
59
|
+
# TODO: spec this
|
60
|
+
case primary_key
|
61
|
+
when String
|
62
|
+
@cache_root << cache_target.send(primary_key.to_sym)
|
63
|
+
when Array
|
64
|
+
@cache_root << primary_key.map { |k| cache_target.send(k.to_sym) }
|
59
65
|
else
|
60
|
-
|
61
|
-
fail ArgumentError, "Could not find key for instance of `#{cache_target.class.name}`, must call with `instance.cached(key: some_unique_key).method`"
|
62
|
-
# rubocop:enable LineLength, StringLiterals
|
66
|
+
fail "Unknown primary key type: #{primary_key.class}"
|
63
67
|
end
|
68
|
+
elsif cache_target.respond_to?(:id)
|
69
|
+
@cache_root << cache_target.send(:id)
|
70
|
+
elsif cache_target.respond_to?(:name)
|
71
|
+
@cache_root << cache_target.send(:name)
|
72
|
+
else
|
73
|
+
# rubocop:disable LineLength, StringLiterals
|
74
|
+
fail ArgumentError, "Could not find key for instance of `#{cache_target.class.name}`, must call with `instance.cached(key: some_unique_key).method`"
|
75
|
+
# rubocop:enable LineLength, StringLiterals
|
64
76
|
end
|
65
77
|
end
|
66
78
|
end
|
data/lib/has_cache/mixin.rb
CHANGED
data/lib/has_cache/version.rb
CHANGED