moderation 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,16 +13,15 @@ class Moderation
13
13
  end
14
14
 
15
15
  def insert(item)
16
- if redis.respond_to?(:multi)
17
- redis.multi { insert_item_and_trim_collection(item) }
18
- else
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 insert_item_and_trim_collection(item)
55
- redis.lpush(collection, item)
56
- redis.ltrim(collection, 0, (limit - 1))
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
@@ -1,3 +1,3 @@
1
1
  class Moderation
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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
- before(:each) do
5
- redis = Redis.new
6
- @redis = Redis::Namespace.new('moderation_test', redis)
7
- @redis.del 'test_data'
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
- it 'initializes with a default limit of 25' do
11
- storage = Moderation::Storage::Redis.new(:collection => 'test_data', :server => @redis)
12
- storage.limit.should eql(25)
13
- end
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
- it 'accepts a custom limit' do
16
- storage = Moderation::Storage::Redis.new(:limit => 200, :collection => 'test_data', :server => @redis)
17
- storage.limit.should eql(200)
18
- end
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
- it 'accespts a new limit after initialization' do
21
- storage = Moderation::Storage::Redis.new(:limit => 200, :collection => 'test_data', :server => @redis)
22
- storage.limit = 135
23
- storage.limit.should eql(135)
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
- it 'inserts data' do
27
- storage = Moderation::Storage::Redis.new(:collection => 'test_data', :server => @redis)
28
- storage.insert('a little bit of data')
29
- storage.all.should eql(['a little bit of data'])
30
- end
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
- it 'returns all data' do
33
- numbers = Moderation::Storage::Redis.new(:collection => 'test_data', :server => @redis)
34
- Array(0..10).each { |n| numbers.insert(n) }
35
- numbers.all.should eql(["10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "0"])
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
- it 'returns a subset of data' do
39
- numbers = Moderation::Storage::Redis.new(:collection => 'test_data', :server => @redis)
40
- Array(0..10).each { |n| numbers.insert(n) }
41
- numbers.all(:limit => 5).should eql(["10", "9", "8", "7", "6"])
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
- it 'returns an empty Array' do
45
- numbers = Moderation::Storage::Redis.new(:collection => 'test_data', :server => @redis)
46
- numbers.all.should eql([])
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
- it 'removes data outside of limit' do
50
- numbers = Moderation::Storage::Redis.new(:collection => 'test_data', :server => @redis, :limit => 5)
51
- Array(0..10).each { |n| numbers.insert(n) }
52
- @redis.lrange('test_data', 0, -1).count.should eql(5)
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.1
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-02-11 00:00:00 Z
13
+ date: 2013-07-20 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multi_json