has_cache 0.1.0 → 0.1.1
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.
- checksums.yaml +4 -4
- data/lib/has_cache/cache.rb +27 -27
- 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: ac5c22719eef48c3a06388262ea7ea328d04ac45
|
4
|
+
data.tar.gz: 926edfbcf821edc0d478da42aa476ea270274503
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47fde97d595b6dd09600e0899647db63a2d975b9f496321254c84b777d28d8d4b46232e6e8c699bf596220de18f603c839739877e11681b9a7cfe8585d3aa7f2
|
7
|
+
data.tar.gz: 0767fc57a5c1b61cdc1af62e30575c546cb042c72c83b96ba1142bf63485398b11c87edaf336c8ded934edda9ae12438e5dab49739d3f84cb6cdbc0a01167d47
|
data/lib/has_cache/cache.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'sourcify'
|
2
2
|
|
3
3
|
module HasCache
|
4
|
-
# The cache class proxies calls to the original
|
5
|
-
# and returns the cached results if passed the same
|
4
|
+
# The cache class proxies calls to the original cache_target, caching the results,
|
5
|
+
# and returns the cached results if passed the same cache_target and arguments
|
6
6
|
class Cache
|
7
7
|
extend HasCache::Utilities
|
8
8
|
include HasCache::Utilities
|
9
9
|
|
10
|
-
attr_accessor :
|
10
|
+
attr_accessor :cache_target, :cache_root, :cache_options
|
11
11
|
|
12
12
|
def self.new(*args, &block)
|
13
13
|
o = allocate
|
@@ -31,34 +31,34 @@ module HasCache
|
|
31
31
|
Rails.cache.delete(key)
|
32
32
|
else
|
33
33
|
Rails.cache.fetch(key, o.cache_options) do
|
34
|
-
if o.
|
35
|
-
result = o.
|
34
|
+
if o.cache_target.is_a?(Class)
|
35
|
+
result = o.cache_target.class_eval(&block)
|
36
36
|
else
|
37
|
-
result = o.
|
37
|
+
result = o.cache_target.instance_eval(&block)
|
38
38
|
end
|
39
39
|
extract_result(result)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
def initialize(
|
45
|
-
@
|
44
|
+
def initialize(cache_target, options = {})
|
45
|
+
@cache_target = cache_target
|
46
46
|
@cache_root = []
|
47
|
-
@cache_options = merged_options(
|
48
|
-
if
|
49
|
-
@cache_root = [
|
47
|
+
@cache_options = merged_options(cache_target, options)
|
48
|
+
if cache_target.is_a?(Class)
|
49
|
+
@cache_root = [cache_target.name, :class]
|
50
50
|
else
|
51
|
-
@cache_root = [
|
52
|
-
unless
|
53
|
-
if
|
54
|
-
@cache_root <<
|
55
|
-
elsif
|
56
|
-
@cache_root <<
|
57
|
-
elsif
|
58
|
-
@cache_root <<
|
51
|
+
@cache_root = [cache_target.class.name, :instance]
|
52
|
+
unless cache_target.respond_to?(:has_cache_key)
|
53
|
+
if cache_target.class.respond_to?(:primary_key)
|
54
|
+
@cache_root << cache_target.send(cache_target.class.send(:primary_key).to_sym)
|
55
|
+
elsif cache_target.respond_to?(:id)
|
56
|
+
@cache_root << cache_target.send(:id)
|
57
|
+
elsif cache_target.respond_to?(:name)
|
58
|
+
@cache_root << cache_target.send(:name)
|
59
59
|
else
|
60
60
|
# rubocop:disable LineLength, StringLiterals
|
61
|
-
fail ArgumentError, "Could not find key for instance of `#{
|
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
62
|
# rubocop:enable LineLength, StringLiterals
|
63
63
|
end
|
64
64
|
end
|
@@ -70,16 +70,16 @@ module HasCache
|
|
70
70
|
if cache_options.key?(:key)
|
71
71
|
options_key = cache_options.delete(:key)
|
72
72
|
if options_key.is_a?(Proc)
|
73
|
-
if
|
74
|
-
key += Array.wrap(
|
73
|
+
if cache_target.is_a?(Class)
|
74
|
+
key += Array.wrap(cache_target.class_eval(&options_key))
|
75
75
|
else
|
76
|
-
key += Array.wrap(
|
76
|
+
key += Array.wrap(cache_target.instance_eval(&options_key))
|
77
77
|
end
|
78
78
|
else
|
79
79
|
key += Array.wrap(options_key)
|
80
80
|
end
|
81
|
-
elsif
|
82
|
-
key +=
|
81
|
+
elsif cache_target.respond_to?(:has_cache_key)
|
82
|
+
key += cache_target.send(:has_cache_key)
|
83
83
|
end
|
84
84
|
key
|
85
85
|
end
|
@@ -96,13 +96,13 @@ module HasCache
|
|
96
96
|
Rails.cache.delete(key)
|
97
97
|
else
|
98
98
|
Rails.cache.fetch(key, cache_options) do
|
99
|
-
extract_result(
|
99
|
+
extract_result(cache_target.send(method, *args))
|
100
100
|
end
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
104
104
|
def respond_to?(method)
|
105
|
-
|
105
|
+
cache_target.respond_to?(method)
|
106
106
|
end
|
107
107
|
end
|
108
108
|
end
|
data/lib/has_cache/version.rb
CHANGED