cache_associations 0.1.2 → 0.2.0

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: 25c9f69129b50855b9f24bd5d22e9b1cbc3a2258
4
- data.tar.gz: 383ec8b76bfe25a25bae015bb690c55b450fc4eb
3
+ metadata.gz: f1dc1a88461b8ab8e50a2f7123a8940380b43b83
4
+ data.tar.gz: c3e7a07d8abff7fe53c1f8fdf3e602bf7b2b3f10
5
5
  SHA512:
6
- metadata.gz: e19b8231d349d5d6560fd2229474db3981909acabcaab381b6f736aaedec8a546467b1bf6979001d172258ec3210808ad45c3740c4861780936074ec45d5df62
7
- data.tar.gz: df928e1a5a1a2f9265db2177282bd9587fd70576fec7dfa43cb1fb067efc0ae9823af0028eb3f83012416503e5dba94d1606129514cdb6ce2d0127c274672bfc
6
+ metadata.gz: 42c3e4fef24eb6b6d85bbe56ce6b0f408b14659c000ed47164c056cc7d8753e3e37d81b6fe2818e205d9d2b94e44603f049b2011089d5a81bc0acc3caca29e17
7
+ data.tar.gz: 1b00a1c669f913857b32e734252b79afcf1e1abc72d2a6f24eea9c3007a0d98364d02e0757df35e7047ef0438ac0a09c55a45c3b68913aa450f29f5e575c3012
@@ -30,6 +30,8 @@ Gem::Specification.new do |spec|
30
30
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
31
  spec.require_paths = ["lib"]
32
32
 
33
+ spec.required_ruby_version = ">= 2.0"
34
+
33
35
  spec.add_dependency "rails", ">= 4.2"
34
36
 
35
37
  spec.add_development_dependency "bundler", "~> 1.15"
@@ -1,3 +1,3 @@
1
1
  module CacheAssociations
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -10,10 +10,11 @@ module CacheAssociations
10
10
  unless reflection = reflect_on_association(name)
11
11
  raise UndefinedAssociationError, "Undefined asscociation #{name}"
12
12
  end
13
+ cache_association_names[name.to_sym] = cache_name_block
13
14
  options = Rails.cache.options.merge(options)
14
15
 
15
16
  define_method("cached_#{name}") do |*args, &block|
16
- cache_name = cache_name_block.nil? ? [self.class.name, id, name, updated_at.to_i] : instance_exec(&cache_name_block)
17
+ cache_name = cache_name_block.nil? ? default_cache_name(name) : instance_exec(&cache_name_block)
17
18
  cache = Rails.cache.fetch(cache_name, **options) do
18
19
  break instance_exec(*args, &block) if !block.nil?
19
20
 
@@ -33,5 +34,45 @@ module CacheAssociations
33
34
  cache
34
35
  end
35
36
  end
37
+
38
+ def has_cache_association?(name)
39
+ cache_association_names.has_key?(name.to_sym)
40
+ end
41
+
42
+ def custom_cache_name?(name)
43
+ !!cache_association_names[name.to_sym]
44
+ end
45
+
46
+ def cache_name_block(name)
47
+ cache_association_names[name.to_sym]
48
+ end
49
+
50
+ private
51
+
52
+ def cache_association_names
53
+ @cache_association_names ||= {}
54
+ end
55
+ end
56
+
57
+ def cache_association_name(name)
58
+ if !!cache_name_block = self.class.cache_name_block(name)
59
+ instance_exec(&cache_name_block)
60
+ elsif self.class.has_cache_association?(name)
61
+ default_cache_name(name)
62
+ else
63
+ nil
64
+ end
65
+ end
66
+
67
+ def clear_caching_on_association(name)
68
+ return nil if !!cache_name = cache_association_name(name)
69
+
70
+ Rails.cache.delete(cache_name)
71
+ end
72
+
73
+ private
74
+
75
+ def default_cache_name(name)
76
+ [self.class.name, id, name.to_s, updated_at.to_i]
36
77
  end
37
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_associations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fuxin Hao
@@ -97,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
97
  requirements:
98
98
  - - ">="
99
99
  - !ruby/object:Gem::Version
100
- version: '0'
100
+ version: '2.0'
101
101
  required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  requirements:
103
103
  - - ">="