moderation 0.0.1 → 0.0.2
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.
- data/lib/moderation/storage/redis.rb +20 -8
- data/lib/moderation/version.rb +1 -1
- data/spec/storage/redis_spec.rb +72 -38
- metadata +2 -2
@@ -13,16 +13,15 @@ class Moderation
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def insert(item)
|
16
|
-
|
17
|
-
redis.
|
18
|
-
|
19
|
-
insert_item_and_trim_collection(item)
|
16
|
+
transaction do
|
17
|
+
redis.lpush(collection, item)
|
18
|
+
redis.ltrim(collection, 0, (limit - 1))
|
20
19
|
end
|
21
20
|
end
|
22
21
|
|
23
22
|
def all(options = {})
|
24
23
|
fetch_limit = options.fetch(:limit, limit).to_i - 1
|
25
|
-
redis.lrange(collection, 0, fetch_limit)
|
24
|
+
redis.lrange(collection, 0, fetch_limit) || []
|
26
25
|
end
|
27
26
|
|
28
27
|
private
|
@@ -51,9 +50,22 @@ class Moderation
|
|
51
50
|
end
|
52
51
|
end
|
53
52
|
|
54
|
-
def
|
55
|
-
|
56
|
-
|
53
|
+
def transaction(&block)
|
54
|
+
if transactions_supported?
|
55
|
+
redis.multi { yield }
|
56
|
+
else
|
57
|
+
yield
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def transactions_supported?
|
62
|
+
@transactions_supported ||= begin
|
63
|
+
redis_version.split('.').first.to_i >= 2 && redis.respond_to?(:multi)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def redis_version
|
68
|
+
@redis_version ||= redis.info["redis_version"]
|
57
69
|
end
|
58
70
|
end
|
59
71
|
end
|
data/lib/moderation/version.rb
CHANGED
data/spec/storage/redis_spec.rb
CHANGED
@@ -1,55 +1,89 @@
|
|
1
1
|
require File.expand_path('../../../lib/moderation/storage/redis.rb', __FILE__)
|
2
2
|
|
3
3
|
describe Moderation::Storage::Redis do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
let(:redis) do
|
5
|
+
client = Redis.new
|
6
|
+
client = Redis::Namespace.new('moderation_test', client)
|
7
|
+
client.del 'test_data'
|
8
|
+
client
|
8
9
|
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
describe '#limit' do
|
12
|
+
it 'initializes with a default limit of 25' do
|
13
|
+
storage = Moderation::Storage::Redis.new(:collection => 'test_data', :server => redis)
|
14
|
+
storage.limit.should eql(25)
|
15
|
+
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
it 'accepts a custom limit' do
|
18
|
+
storage = Moderation::Storage::Redis.new(:limit => 200, :collection => 'test_data', :server => redis)
|
19
|
+
storage.limit.should eql(200)
|
20
|
+
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
it 'accespts a new limit after initialization' do
|
23
|
+
storage = Moderation::Storage::Redis.new(:limit => 200, :collection => 'test_data', :server => redis)
|
24
|
+
storage.limit = 135
|
25
|
+
storage.limit.should eql(135)
|
26
|
+
end
|
24
27
|
end
|
25
28
|
|
26
|
-
|
27
|
-
storage
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
describe '#insert' do
|
30
|
+
let(:storage) { Moderation::Storage::Redis.new(
|
31
|
+
:collection => 'test_data',
|
32
|
+
:server => redis,
|
33
|
+
:limit => 5)
|
34
|
+
}
|
31
35
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
36
|
+
it 'stores data' do
|
37
|
+
storage.insert('a little bit of data')
|
38
|
+
storage.all.should eql(['a little bit of data'])
|
39
|
+
end
|
37
40
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
41
|
+
it 'removes data outside of limit' do
|
42
|
+
Array(0..10).each { |n| storage.insert(n) }
|
43
|
+
redis.lrange('test_data', 0, -1).count.should eql(5)
|
44
|
+
end
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
-
|
46
|
+
it 'uses a transaction with redis 2.x or greater' do
|
47
|
+
redis.stub(:info).and_return({"redis_version" => "2.6.4"})
|
48
|
+
redis.stub(:lpush)
|
49
|
+
redis.stub(:ltrim)
|
50
|
+
redis.should_receive(:multi).and_yield
|
51
|
+
storage.insert('more data')
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'does not use a transaction with redis 1.x' do
|
55
|
+
redis.stub(:info).and_return({"redis_version" => "1.2.0"})
|
56
|
+
redis.stub(:lpush)
|
57
|
+
redis.stub(:ltrim)
|
58
|
+
redis.should_not_receive(:multi)
|
59
|
+
storage.insert('more data')
|
60
|
+
end
|
47
61
|
end
|
48
62
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
63
|
+
describe '#all' do
|
64
|
+
it 'returns all data' do
|
65
|
+
numbers = Moderation::Storage::Redis.new(:collection => 'test_data', :server => redis)
|
66
|
+
Array(0..10).each { |n| numbers.insert(n) }
|
67
|
+
numbers.all.should eql(["10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "0"])
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'returns a subset of data' do
|
71
|
+
numbers = Moderation::Storage::Redis.new(:collection => 'test_data', :server => redis)
|
72
|
+
Array(0..10).each { |n| numbers.insert(n) }
|
73
|
+
numbers.all(:limit => 5).should eql(["10", "9", "8", "7", "6"])
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'returns an empty Array with redis greater than 2.x' do
|
77
|
+
redis.should_receive(:lrange).with('test_data', 0, 24).and_return([])
|
78
|
+
numbers = Moderation::Storage::Redis.new(:collection => 'test_data', :server => redis)
|
79
|
+
numbers.all.should eql([])
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'returns an empty Array with redis 1.x' do
|
83
|
+
redis.should_receive(:lrange).with('test_data', 0, 24).and_return(nil)
|
84
|
+
numbers = Moderation::Storage::Redis.new(:collection => 'test_data', :server => redis)
|
85
|
+
numbers.all.should eql([])
|
86
|
+
end
|
53
87
|
end
|
54
88
|
end
|
55
89
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: moderation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Lee Jones
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-07-20 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: multi_json
|