restrict_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: e4a37b2b0b86aaf69fda3297713e32fdd1951722
4
- data.tar.gz: f221ff5b19e8c16e2844d0e45e70aa1989226ac8
3
+ metadata.gz: 1c29fdf424ebafc1734aacbc3da69ca1a5ff9563
4
+ data.tar.gz: 48cc0cd115be5e997c74763b02569a7c8a978b17
5
5
  SHA512:
6
- metadata.gz: a80b5f4e4070bf06a1bd4ec645c1d05f320f5b69dce451779c524677168b5578dab87744061d127502185cad5171677f7a8775164f7ffec1a4df1053ba0a8cda
7
- data.tar.gz: 2833d2da4631d6873c4e7b5fc8ba3a5f6809850d7f78a0e12e1432851a5752740e3c9969fb47ccb5f6328a43bbe4f3178b211488ff6d6928c416d3c7870adec7
6
+ metadata.gz: c29896eacf36a4e75f08752e1119ac5f65599894191ce44ee005790782d2164dd28a186df8fd37dc7e729ff4548c2350a1c208a2eca39642ce43ce8381bb9d93
7
+ data.tar.gz: 680747417162f543812e2ce6c34bca13b13d5847f6fe8e746cbd0b26c1329f3ba1cbba12e806c701964aa3f1796457e914e7685e99ff446ed27ea33d75781581
data/README.md CHANGED
@@ -34,6 +34,15 @@ end
34
34
 
35
35
  #### Callbacks (Around Action)
36
36
 
37
+ ```ruby
38
+ class MyController < ApplicationController
39
+ # cache_sweep_action :index, :show
40
+ cache_sweep_action
41
+ end
42
+ ```
43
+
44
+ #### ActiveRecord Cache
45
+
37
46
  ```ruby
38
47
  class MyModel < ActiveRecord::Base; end
39
48
  MyModel.find_with_restrict_cache(id_or_ids)
@@ -2,13 +2,23 @@ module RestrictCache
2
2
  module Accessible
3
3
  THREAD_KEY = :restrict_cache
4
4
 
5
- def collection
5
+ def cache
6
6
  Thread.current[THREAD_KEY] ||= Cacheable.new
7
7
  end
8
8
 
9
9
  def clear
10
10
  Thread.current[THREAD_KEY] = nil
11
11
  end
12
+
13
+ def method_missing(name, *args, &block)
14
+ super unless cache.respond_to?(name)
15
+
16
+ define_singleton_method(name) do |*a, &b|
17
+ cache.public_send(name, *a, &b)
18
+ end
19
+
20
+ send(name, *args, &block)
21
+ end
12
22
  end
13
23
 
14
24
  extend Accessible
@@ -1,5 +1,5 @@
1
1
  module RestrictCache
2
- class ActionControllerExt
2
+ module ActionControllerExt
3
3
  include ActiveSupport::Concern
4
4
 
5
5
  module ClassMethods
@@ -1,10 +1,20 @@
1
1
  module RestrictCache
2
2
  module ActiveRecordExt
3
- def self.included(base)
4
- base.extend ClassMethods
3
+ module Base
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ delegate :find_with_restrict_cache, to: :all
8
+ end
9
+
10
+ def restrict_cache
11
+ RestrictCache.add(self)
12
+ end
5
13
  end
6
14
 
7
- module ClassMethods
15
+ module Relation
16
+ AR_CACHE_KEY = Cacheable::CacheKey::ACTIVERECORD
17
+
8
18
  def find_and_restrict_cache(arg)
9
19
  records = find(arg)
10
20
  Array(records).each(&:restrict_cache)
@@ -12,7 +22,7 @@ module RestrictCache
12
22
  end
13
23
 
14
24
  def find_from_restrict_cache(arg)
15
- contents = RestrictCache.collection.active_record.contents(self.table_name)
25
+ contents = RestrictCache.send(AR_CACHE_KEY).contents(self.table_name)
16
26
  return nil unless contents
17
27
 
18
28
  case arg
@@ -36,18 +46,19 @@ module RestrictCache
36
46
  end
37
47
  end
38
48
 
49
+ def with_restrict_cache
50
+ self.each(&:restrict_cache)
51
+ self
52
+ end
53
+
39
54
  private
40
55
  def restrict_cached?(args)
41
- content = RestrictCache.collection.active_record.contents(self.table_name)
56
+ content = RestrictCache.send(AR_CACHE_KEY).contents(self.table_name)
42
57
  return false unless content
43
58
  ids = content.keys
44
59
  args.all? {|index| ids.include?(index) }
45
60
  end
46
61
  end
47
-
48
- def restrict_cache
49
- RestrictCache.collection.add(self)
50
- end
51
62
  end
52
63
  end
53
64
 
@@ -10,10 +10,12 @@ module RestrictCache
10
10
  ALL = [ACTIVERECORD, CUSTOM]
11
11
 
12
12
  def self.get(content)
13
- if defined?(ActiveRecord) && content.class < ActiveRecord::Base
14
- return ACTIVERECORD
13
+ case
14
+ when defined?(ActiveRecord) && content.class < ActiveRecord::Base
15
+ ACTIVERECORD
16
+ else
17
+ CUSTOM
15
18
  end
16
- CUSTOM
17
19
  end
18
20
  end
19
21
 
@@ -9,7 +9,8 @@ module RestrictCache
9
9
  end
10
10
 
11
11
  ActiveSupport.on_load(:active_record) do
12
- ::ActiveRecord::Base.send(:include, RestrictCache::ActiveRecordExt)
12
+ ::ActiveRecord::Base.send(:include, RestrictCache::ActiveRecordExt::Base)
13
+ ::ActiveRecord::Relation.send(:include, RestrictCache::ActiveRecordExt::Relation)
13
14
  end
14
15
  end
15
16
  end
@@ -1,3 +1,4 @@
1
1
  module RestrictCache
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
4
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restrict_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
  - i2bskn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-03-14 00:00:00.000000000 Z
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler