counter-cache 0.0.2 → 0.1.0
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 +4 -4
- data/README.md +14 -0
- data/lib/counter/cache/counters/buffer_counter/updater.rb +7 -2
- data/lib/counter/cache/options_parser.rb +4 -0
- data/lib/counter/cache/redis.rb +4 -4
- data/lib/counter/cache/version.rb +1 -1
- data/spec/features/counter_spec.rb +7 -0
- data/spec/lib/counter/cache/buffer_counter/updater_spec.rb +1 -1
- data/spec/lib/counter/cache/redis_spec.rb +12 -2
- data/spec/support/models.rb +20 -0
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65f431cac9a84b8abe458919608c07c66501c68e
|
4
|
+
data.tar.gz: c0582fc1f4d829fc50162ce50746b33fdf06f2c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db21a9548ac98191ddf559ff962c78f221656998efbe5f1a0c2af14e153e7256647fa3f54bc4f00faa8b3d34b9feabd9f94d6f82410805b241eb3384593ba1a9
|
7
|
+
data.tar.gz: 8b4abafb442cbdedef2ddffae83ebc7d71a418dedbef791ad3f963abc7c039b45e44fa8d4d2d992514da60091e8d555932439555c319f33923ac0fc12271f5a4
|
data/README.md
CHANGED
@@ -109,6 +109,20 @@ class Post
|
|
109
109
|
end
|
110
110
|
```
|
111
111
|
|
112
|
+
#### To increment dynamically:
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
class Review
|
116
|
+
include Counter::Cache
|
117
|
+
|
118
|
+
counter_cache_on column: :reviews_sum,
|
119
|
+
relation: :product,
|
120
|
+
relation_class_name: "Product",
|
121
|
+
increment_by: ->(review) { review.score },
|
122
|
+
method: :recalculate_reviews_sum, # This is a method on the product.
|
123
|
+
end
|
124
|
+
```
|
125
|
+
|
112
126
|
#### To control when recalculation happens:
|
113
127
|
|
114
128
|
```ruby
|
@@ -33,11 +33,16 @@ module Counter
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def incr
|
36
|
-
redis.incr(key)
|
36
|
+
redis.incr(key, increment_by)
|
37
37
|
end
|
38
38
|
|
39
39
|
def decr
|
40
|
-
redis.decr(key)
|
40
|
+
redis.decr(key, increment_by)
|
41
|
+
end
|
42
|
+
|
43
|
+
def increment_by
|
44
|
+
return options.increment_by.call(source_object) if options.increment_by.is_a?(Proc)
|
45
|
+
1
|
41
46
|
end
|
42
47
|
|
43
48
|
def key
|
data/lib/counter/cache/redis.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
module Counter
|
2
2
|
module Cache
|
3
3
|
class Redis
|
4
|
-
def incr(key)
|
4
|
+
def incr(key, val = 1)
|
5
5
|
with_redis do |redis|
|
6
|
-
redis.
|
6
|
+
redis.incrby key, val
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
def decr(key)
|
10
|
+
def decr(key, val = 1)
|
11
11
|
with_redis do |redis|
|
12
|
-
redis.
|
12
|
+
redis.decrby(key, val)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -91,5 +91,12 @@ RSpec.describe "Counting" do
|
|
91
91
|
expect(user.reload.bogus_followed_count).to eq(101)
|
92
92
|
end
|
93
93
|
end
|
94
|
+
|
95
|
+
describe '#reviews_sum' do
|
96
|
+
it 'increments the user review sum by the review score' do
|
97
|
+
Review.create(score: 4, user: user)
|
98
|
+
expect(user.reload.reviews_sum).to eq(4)
|
99
|
+
end
|
100
|
+
end
|
94
101
|
end
|
95
102
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe Counter::Cache::Counters::BufferCounter::Updater do
|
4
|
-
let(:options) { double(relation: "boo", relation_class_name: "Boo", column: "boo", relation_id: nil) }
|
4
|
+
let(:options) { double(relation: "boo", relation_class_name: "Boo", column: "boo", relation_id: nil, increment_by: 1) }
|
5
5
|
let(:source_object) { double(boo_id: 1) }
|
6
6
|
let(:updater) { Counter::Cache::Counters::BufferCounter::Updater.new(source_object, options, "Hello") }
|
7
7
|
|
@@ -16,16 +16,26 @@ RSpec.describe Counter::Cache::Redis do
|
|
16
16
|
|
17
17
|
describe '#decr' do
|
18
18
|
it 'calls decr on redis with the key' do
|
19
|
-
expect(redis).to receive(:
|
19
|
+
expect(redis).to receive(:decrby).with("hello", 1)
|
20
20
|
helper.decr("hello")
|
21
21
|
end
|
22
|
+
|
23
|
+
it 'calls decr on redis with the key and increment value' do
|
24
|
+
expect(redis).to receive(:decrby).with("hello", 2)
|
25
|
+
helper.decr("hello", 2)
|
26
|
+
end
|
22
27
|
end
|
23
28
|
|
24
29
|
describe '#incr' do
|
25
30
|
it 'calls incr on redis with the key' do
|
26
|
-
expect(redis).to receive(:
|
31
|
+
expect(redis).to receive(:incrby).with("hello", 1)
|
27
32
|
helper.incr("hello")
|
28
33
|
end
|
34
|
+
|
35
|
+
it 'calls incr on redis with the key and increment value' do
|
36
|
+
expect(redis).to receive(:incrby).with("hello", 2)
|
37
|
+
helper.incr("hello", 2)
|
38
|
+
end
|
29
39
|
end
|
30
40
|
|
31
41
|
describe '#del' do
|
data/spec/support/models.rb
CHANGED
@@ -12,6 +12,7 @@ class CreateModelsForTest < ActiveRecord::Migration
|
|
12
12
|
t.integer :followers_count, :default => 0
|
13
13
|
t.integer :users_i_follow_count, :default => 0
|
14
14
|
t.integer :bogus_followed_count, :default => 0
|
15
|
+
t.integer :reviews_sum, :default => 0
|
15
16
|
end
|
16
17
|
|
17
18
|
create_table :follows do |t|
|
@@ -24,17 +25,24 @@ class CreateModelsForTest < ActiveRecord::Migration
|
|
24
25
|
t.string :body
|
25
26
|
t.belongs_to :user
|
26
27
|
end
|
28
|
+
|
29
|
+
create_table :reviews do |t|
|
30
|
+
t.integer :score
|
31
|
+
t.belongs_to :user
|
32
|
+
end
|
27
33
|
end
|
28
34
|
|
29
35
|
def self.down
|
30
36
|
drop_table(:users)
|
31
37
|
drop_table(:posts)
|
32
38
|
drop_table(:follows)
|
39
|
+
drop_table(:reviews)
|
33
40
|
end
|
34
41
|
end
|
35
42
|
|
36
43
|
class User < ActiveRecord::Base
|
37
44
|
has_many :posts
|
45
|
+
has_many :reviews
|
38
46
|
|
39
47
|
def calculate_posts_count
|
40
48
|
posts.count
|
@@ -85,3 +93,15 @@ class Post < ActiveRecord::Base
|
|
85
93
|
relation_class_name: "User",
|
86
94
|
recalculation: false
|
87
95
|
end
|
96
|
+
|
97
|
+
class Review < ActiveRecord::Base
|
98
|
+
belongs_to :user
|
99
|
+
|
100
|
+
include Counter::Cache
|
101
|
+
|
102
|
+
counter_cache_on column: :reviews_sum,
|
103
|
+
relation: :user,
|
104
|
+
increment_by: ->(review) { review.score },
|
105
|
+
recalculation: false
|
106
|
+
|
107
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: counter-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Henry & Matt Camuto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -207,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
207
207
|
version: '0'
|
208
208
|
requirements: []
|
209
209
|
rubyforge_project:
|
210
|
-
rubygems_version: 2.2.
|
210
|
+
rubygems_version: 2.2.2
|
211
211
|
signing_key:
|
212
212
|
specification_version: 4
|
213
213
|
summary: Counting is hard.
|
@@ -227,4 +227,3 @@ test_files:
|
|
227
227
|
- spec/spec_helper.rb
|
228
228
|
- spec/support/models.rb
|
229
229
|
- spec/support/worker_adapter.rb
|
230
|
-
has_rdoc:
|