volatile_counter_cache 0.0.1 → 0.0.2

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: 8269703b195a013b43674ac19dd4a7bd7e659a87
4
- data.tar.gz: 65704e49e6dad06b31f4cc4ce704938fb570e407
3
+ metadata.gz: 0dc0bb5209cea0920a758459442c6fac1cec1df9
4
+ data.tar.gz: 9f66c42130cf13c202640c9790f9a885364ffaee
5
5
  SHA512:
6
- metadata.gz: 3e6dcf87c79f3f26817f6377154cb620c6754fa5d42e6aa7dc5da99a995828e3b8b044a935ced5c86e21f4e79058bdefe17d4236221d8c9cff1c124dc208961b
7
- data.tar.gz: 0109f3d4c1b22b1d9e519769e733299773c04d01d415c0b90a04f5638f9393c34826ef0e34d78dba2c635830b8dcc4802e6d610e764e2357762ca168fb93fa3c
6
+ metadata.gz: eedac8adabcaf4e824a70ee78a9c19b8ece9cd3279c31f67de2d7cf28e483071d22fcac4517dbab255b8425c90090dc654af4242485c38371d3ee5e3bdc0b5fb
7
+ data.tar.gz: 5502399435fdc3c46bbe682bc1ebdbc031dd62849ec604403ea0adce3513154ff4ebee9c39f83d8cb6f541f2fa5be4f5efafbb2f42663fd65f745c40e77604e9
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## 0.0.2
2
+ - Extend `ActiveRecord::Base`.
3
+
4
+ ## 0.0.1
5
+ - 1st release.
data/README.md CHANGED
@@ -8,8 +8,6 @@ Provides volatile counter cache logic to ActiveRecord::Base.
8
8
 
9
9
  ```ruby
10
10
  class Tweet < ActiveRecord::Base
11
- include VolatileCounterCache
12
-
13
11
  has_many :favorites
14
12
 
15
13
  volatile_counter_cache :favorites, cache: Rails.cache
@@ -0,0 +1,3 @@
1
+ ActiveSupport.on_load(:active_record) do
2
+ ActiveRecord::Base.extend(VolatileCounterCache::ClassMethods)
3
+ end
@@ -0,0 +1,44 @@
1
+ # A model concern module to provide volatile counter cache logic to model class.
2
+ #
3
+ # @example
4
+ # class Tweet < ActiveRecord::Base
5
+ # include VolatileCounterCache
6
+ # has_many :favorites
7
+ # volatile_counter_cache :favorites, cache: Rails.cache
8
+ # end
9
+ # Tweet.first.favorites_count #=> 42
10
+ # Tweet.first.favorites.first.destroy
11
+ # Tweet.first.favorites_count #=> 41
12
+ #
13
+ module VolatileCounterCache
14
+ CACHE_KEY_PREFIX = 'volatile-counter-cache'
15
+
16
+ extend ActiveSupport::Concern
17
+
18
+ module ClassMethods
19
+ # Define counter method and callback using given cache store for 1:N association.
20
+ # @param association_name [Symbol] Association name used to counter method, cache key, and counting.
21
+ # @param cache [ActiveSupport::Cache::Store] A store object to store count for each key.
22
+ # @param cache_options [Hash] Options for cache store.
23
+ # @param counter_method_name [String] An optional String to change counter method name.
24
+ # @param foreign_key [Symbol] An optional Symbol to change cache key at callback.
25
+ def volatile_counter_cache(association_name, cache:, cache_options: {}, counter_method_name: nil, foreign_key: nil)
26
+ counter_method_name = counter_method_name || "#{association_name}_count"
27
+ cache_key_prefix = "#{CACHE_KEY_PREFIX}/#{model_name}/#{association_name}"
28
+
29
+ define_method(counter_method_name) do
30
+ cache.fetch("#{cache_key_prefix}/#{id}", cache_options) do
31
+ send(association_name).count
32
+ end
33
+ end
34
+
35
+ foreign_key = foreign_key || "#{table_name.singularize }_id"
36
+ child_class = association_name.to_s.singularize.camelize.constantize
37
+ callback_proc = -> do
38
+ cache.delete("#{cache_key_prefix}/#{send(foreign_key)}", cache_options)
39
+ end
40
+ child_class.after_create(&callback_proc)
41
+ child_class.after_destroy(&callback_proc)
42
+ end
43
+ end
44
+ end
@@ -1,3 +1,3 @@
1
1
  module VolatileCounterCache
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,46 +1,3 @@
1
1
  require "volatile_counter_cache/version"
2
-
3
- # A model concern module to provide volatile counter cache logic to model class.
4
- #
5
- # @example
6
- # class Tweet < ActiveRecord::Base
7
- # include VolatileCounterCache
8
- # has_many :favorites
9
- # volatile_counter_cache :favorites, cache: Rails.cache
10
- # end
11
- # Tweet.first.favorites_count #=> 42
12
- # Tweet.first.favorites.first.destroy
13
- # Tweet.first.favorites_count #=> 41
14
- #
15
- module VolatileCounterCache
16
- CACHE_KEY_PREFIX = 'volatile-counter-cache'
17
-
18
- extend ActiveSupport::Concern
19
-
20
- module ClassMethods
21
- # Define counter method and callback using given cache store for 1:N association.
22
- # @param association_name [Symbol] Association name used to counter method, cache key, and counting.
23
- # @param cache [ActiveSupport::Cache::Store] A store object to store count for each key.
24
- # @param cache_options [Hash] Options for cache store.
25
- # @param counter_method_name [String] An optional String to change counter method name.
26
- # @param foreign_key [Symbol] An optional Symbol to change cache key at callback.
27
- def volatile_counter_cache(association_name, cache:, cache_options: {}, counter_method_name: nil, foreign_key: nil)
28
- counter_method_name = counter_method_name || "#{association_name}_count"
29
- cache_key_prefix = "#{CACHE_KEY_PREFIX}/#{model_name}/#{association_name}"
30
-
31
- define_method(counter_method_name) do
32
- cache.fetch("#{cache_key_prefix}/#{id}", cache_options) do
33
- send(association_name).count
34
- end
35
- end
36
-
37
- foreign_key = foreign_key || "#{table_name.singularize }_id"
38
- child_class = association_name.to_s.singularize.camelize.constantize
39
- callback_proc = -> do
40
- cache.delete("#{cache_key_prefix}/#{send(foreign_key)}", cache_options)
41
- end
42
- child_class.after_create(&callback_proc)
43
- child_class.after_destroy(&callback_proc)
44
- end
45
- end
46
- end
2
+ require "volatile_counter_cache/class_methods"
3
+ require "volatile_counter_cache/active_record"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: volatile_counter_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -46,11 +46,14 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
+ - CHANGELOG.md
49
50
  - Gemfile
50
51
  - LICENSE.txt
51
52
  - README.md
52
53
  - Rakefile
53
54
  - lib/volatile_counter_cache.rb
55
+ - lib/volatile_counter_cache/active_record.rb
56
+ - lib/volatile_counter_cache/class_methods.rb
54
57
  - lib/volatile_counter_cache/version.rb
55
58
  - volatile_counter_cache.gemspec
56
59
  homepage: https://github.com/r7kamura/volatile_counter_cache