has_cache 0.1.0 → 0.1.1

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: f097af5dce6e37cb73c4780e8cd16f278e374451
4
- data.tar.gz: 8a62ceaa33422e9b19e1b27ee583a77275791451
3
+ metadata.gz: ac5c22719eef48c3a06388262ea7ea328d04ac45
4
+ data.tar.gz: 926edfbcf821edc0d478da42aa476ea270274503
5
5
  SHA512:
6
- metadata.gz: 979c91b0b8f95f777b4542a481db3d71503e9e1f2d3bdbf034e178756ca7f5d9f639bcbd213ba65fc49765599b3e63d2fca59d296e59c5ddee5f5351f0f48488
7
- data.tar.gz: fccff51b978e8ca8b580201b6f407c1041f1f384ddff0b7ca76181ed323eb11a425dd08f516e94aef25ef8707516176293d31b9203c3d2aceb3ff6f9738b68f1
6
+ metadata.gz: 47fde97d595b6dd09600e0899647db63a2d975b9f496321254c84b777d28d8d4b46232e6e8c699bf596220de18f603c839739877e11681b9a7cfe8585d3aa7f2
7
+ data.tar.gz: 0767fc57a5c1b61cdc1af62e30575c546cb042c72c83b96ba1142bf63485398b11c87edaf336c8ded934edda9ae12438e5dab49739d3f84cb6cdbc0a01167d47
@@ -1,13 +1,13 @@
1
1
  require 'sourcify'
2
2
 
3
3
  module HasCache
4
- # The cache class proxies calls to the original target, caching the results,
5
- # and returns the cached results if passed the same target and arguments
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 :target, :cache_root, :cache_options
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.target.is_a?(Class)
35
- result = o.target.class_eval(&block)
34
+ if o.cache_target.is_a?(Class)
35
+ result = o.cache_target.class_eval(&block)
36
36
  else
37
- result = o.target.instance_eval(&block)
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(target, options = {})
45
- @target = target
44
+ def initialize(cache_target, options = {})
45
+ @cache_target = cache_target
46
46
  @cache_root = []
47
- @cache_options = merged_options(target, options)
48
- if target.is_a?(Class)
49
- @cache_root = [target.name, :class]
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 = [target.class.name, :instance]
52
- unless target.respond_to?(:has_cache_key)
53
- if target.class.respond_to?(:primary_key)
54
- @cache_root << target.send(target.class.send(:primary_key).to_sym)
55
- elsif target.respond_to?(:id)
56
- @cache_root << target.send(:id)
57
- elsif target.respond_to?(:name)
58
- @cache_root << target.send(:name)
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 `#{target.class.name}`, must call with `instance.cached(key: some_unique_key).method`"
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 target.is_a?(Class)
74
- key += Array.wrap(target.class_eval(&options_key))
73
+ if cache_target.is_a?(Class)
74
+ key += Array.wrap(cache_target.class_eval(&options_key))
75
75
  else
76
- key += Array.wrap(target.instance_eval(&options_key))
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 target.respond_to?(:has_cache_key)
82
- key += target.send(:has_cache_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(target.send(method, *args))
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
- target.respond_to?(method)
105
+ cache_target.respond_to?(method)
106
106
  end
107
107
  end
108
108
  end
@@ -1,4 +1,4 @@
1
1
  # @private
2
2
  module HasCache
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Fern