has_cache 0.1.3 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea1ef981395474d7babad8f7d6eb83ffef1e88d2
4
- data.tar.gz: fac756b188a047e5d7fc48167e7afc77a69d4ec5
3
+ metadata.gz: f826a005438e6e2239208a8dd6e7e320a8f00d12
4
+ data.tar.gz: 257aabcfca729a031f47cd32dfa598c43fa12a52
5
5
  SHA512:
6
- metadata.gz: ffbdadbdd6f99015823e5d4e474d61db7a70c80c89420e0259764032092d5179d2a7083fbed2aea70fdb39e13c367a5b3bc542e72fa58bb0dfbed2ba22db7787
7
- data.tar.gz: bbc3e9abff9d05fd1f39339a9d7c83bcd911c2b966e7f0e9510c6a21870a103773b383034d711a224e31d7f42cb2211b29b4cbaf32428adba25af0bd17444f8b
6
+ metadata.gz: ad0e0a76f1797361e988e6b4601da3690c21dd157550237c022c0d05528ec98c3847c90f5fc4e6651857af89601e1272afa968a86e2a4e583f54281bb1e59e0d
7
+ data.tar.gz: f2d655273163d28ef458592eade0cc6d047e5f1adb91ae6efcd5243a0e012645b6f1a8243f67e382c4d797d2e1d4a6a3e0be801977eeccfba72f9268185188bf
@@ -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 results,
5
- # and returns the cached results if passed the same cache_target and arguments
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
- result = o.cache_target.class_eval(&block)
36
+ extract_result(o.cache_target.class_eval(&block))
36
37
  else
37
- result = o.cache_target.instance_eval(&block)
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
- 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)
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
- # rubocop:disable LineLength, StringLiterals
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
@@ -24,7 +24,7 @@ module HasCache
24
24
  end
25
25
 
26
26
  def delete_cached(*args, &block)
27
- args.push :delete => true
27
+ args.push(delete: true)
28
28
  HasCache::Cache.new(self, *args, &block)
29
29
  end
30
30
  end
@@ -1,4 +1,4 @@
1
1
  # @private
2
2
  module HasCache
3
- VERSION = '0.1.3'
3
+ VERSION = '0.1.4'
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.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Fern