redness 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/Gemfile +1 -7
- data/lib/redness/red.rb +7 -1
- data/lib/redness/red_hash.rb +4 -0
- data/lib/redness/red_list.rb +17 -0
- data/lib/redness/red_set.rb +26 -2
- data/lib/{version.rb → redness/version.rb} +1 -1
- data/redness.gemspec +7 -6
- data/spec/redness/red_hash_spec.rb +16 -0
- data/spec/redness/red_list_spec.rb +45 -0
- data/spec/redness/red_set_spec.rb +67 -10
- metadata +59 -17
- data/Gemfile.lock +0 -32
data/.gitignore
CHANGED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
CHANGED
data/lib/redness/red.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'timeout'
|
2
|
+
|
1
3
|
class Red
|
2
4
|
class RedisUnavailable < StandardError; end
|
3
5
|
|
@@ -5,6 +7,10 @@ class Red
|
|
5
7
|
$redis
|
6
8
|
end
|
7
9
|
|
10
|
+
def self.client_version
|
11
|
+
@version ||= Redis::VERSION.scan(/\d+/).map { |s| s.to_i }
|
12
|
+
end
|
13
|
+
|
8
14
|
def self.delete(key)
|
9
15
|
redis.del(key)
|
10
16
|
end
|
@@ -15,7 +21,7 @@ class Red
|
|
15
21
|
|
16
22
|
def execute_with_uncertainty(fail_return = [])
|
17
23
|
yield
|
18
|
-
rescue RedisUnavailable, Errno::ECONNREFUSED, Timeout, Timeout::Error, Errno::EAGAIN
|
24
|
+
rescue RedisUnavailable, Redis::CannotConnectError, Errno::ECONNREFUSED, Timeout, Timeout::Error, Errno::EAGAIN
|
19
25
|
fail_return
|
20
26
|
end
|
21
27
|
|
data/lib/redness/red_hash.rb
CHANGED
data/lib/redness/red_list.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
class RedList
|
2
|
+
attr_reader :key
|
3
|
+
|
2
4
|
def self.redis
|
3
5
|
@redis ||= Red.new
|
4
6
|
end
|
@@ -47,4 +49,19 @@ class RedList
|
|
47
49
|
end
|
48
50
|
end
|
49
51
|
|
52
|
+
def initialize(key)
|
53
|
+
@key = key
|
54
|
+
end
|
55
|
+
|
56
|
+
def add(value)
|
57
|
+
self.class.add(@key, value)
|
58
|
+
end
|
59
|
+
|
60
|
+
def remove(value)
|
61
|
+
self.class.remove(@key, value)
|
62
|
+
end
|
63
|
+
|
64
|
+
def value
|
65
|
+
self.class.get(@key)
|
66
|
+
end
|
50
67
|
end
|
data/lib/redness/red_set.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
class RedSet
|
2
|
+
attr_reader :key
|
3
|
+
|
2
4
|
def self.redis
|
3
5
|
@redis ||= Red.new
|
4
6
|
end
|
@@ -43,8 +45,14 @@ class RedSet
|
|
43
45
|
results = redis.zrevrange(key, lower_bound, upper_bound, :with_scores => with_scores)
|
44
46
|
if with_scores
|
45
47
|
[].tap do |memo|
|
46
|
-
|
47
|
-
|
48
|
+
if Red.client_version.first < 3
|
49
|
+
results.each_slice(2) do |slice|
|
50
|
+
memo << [slice[0].to_i, scoring.call(slice[1].to_i)]
|
51
|
+
end
|
52
|
+
else
|
53
|
+
results.each do |member, score|
|
54
|
+
memo << [member.to_i, scoring.call(score)]
|
55
|
+
end
|
48
56
|
end
|
49
57
|
end
|
50
58
|
else
|
@@ -73,4 +81,20 @@ class RedSet
|
|
73
81
|
redis.zscore(key, member)
|
74
82
|
end
|
75
83
|
end
|
84
|
+
|
85
|
+
def initialize(key)
|
86
|
+
@key = key
|
87
|
+
end
|
88
|
+
|
89
|
+
def add(value, options = {})
|
90
|
+
self.class.add(@key, value, options)
|
91
|
+
end
|
92
|
+
|
93
|
+
def value
|
94
|
+
self.class.get(@key)
|
95
|
+
end
|
96
|
+
|
97
|
+
def remove(value)
|
98
|
+
self.class.remove(@key, value)
|
99
|
+
end
|
76
100
|
end
|
data/redness.gemspec
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
$:.push File.expand_path("../lib", __FILE__)
|
2
|
-
require "version"
|
2
|
+
require "redness/version"
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "redness"
|
@@ -18,10 +18,11 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.homepage = "http://github.com/howaboutwe/redness"
|
19
19
|
s.licenses = ["MIT"]
|
20
20
|
s.files = `git ls-files`.split("\n")
|
21
|
-
s.
|
22
|
-
s.
|
23
|
-
s.
|
24
|
-
s.
|
25
|
-
s.
|
21
|
+
s.add_runtime_dependency "json"
|
22
|
+
s.add_runtime_dependency "rspec"
|
23
|
+
s.add_runtime_dependency "redis", "~> 3.0.0"
|
24
|
+
s.add_runtime_dependency "activesupport"
|
25
|
+
s.add_development_dependency "timecop"
|
26
|
+
s.add_development_dependency "rake"
|
26
27
|
s.require_paths = ["lib"]
|
27
28
|
end
|
@@ -75,6 +75,22 @@ describe RedHash do
|
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
+
describe "#get" do
|
79
|
+
before do
|
80
|
+
red_hash.set(
|
81
|
+
key: "foo",
|
82
|
+
another: "bar",
|
83
|
+
yet_another: "baz"
|
84
|
+
)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "is aliased to []" do
|
88
|
+
red_hash.should_receive(:[]).with("key").and_return("foo")
|
89
|
+
|
90
|
+
red_hash.get("key").should == "foo"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
78
94
|
describe "#set" do
|
79
95
|
it "accepts a hash, and sets all keys" do
|
80
96
|
red_hash.set(
|
@@ -1,6 +1,51 @@
|
|
1
1
|
require 'spec_integration_helper'
|
2
2
|
|
3
3
|
describe RedList do
|
4
|
+
describe "#initialize" do
|
5
|
+
it "creates a new RedList instance with the provided key" do
|
6
|
+
list = RedList.new("somekey")
|
7
|
+
|
8
|
+
list.key.should == "somekey"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#add" do
|
13
|
+
it "adds the element to the list" do
|
14
|
+
list = RedList.new("somekey")
|
15
|
+
|
16
|
+
list.add(1)
|
17
|
+
|
18
|
+
list.value.should == [1]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#remove" do
|
23
|
+
it "removes the element from the list" do
|
24
|
+
list = RedList.new("somekey")
|
25
|
+
|
26
|
+
list.add(1)
|
27
|
+
list.add(2)
|
28
|
+
|
29
|
+
list.value.should == [2, 1]
|
30
|
+
|
31
|
+
list.remove(2)
|
32
|
+
|
33
|
+
list.value.should == [1]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#value" do
|
38
|
+
it "returns the vlue of the list" do
|
39
|
+
list = RedList.new("somekey")
|
40
|
+
|
41
|
+
list.value.should == []
|
42
|
+
|
43
|
+
list.add(1)
|
44
|
+
|
45
|
+
list.value.should == [1]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
4
49
|
describe ".add" do
|
5
50
|
it "adds a member to the list" do
|
6
51
|
RedList.add("somekey", 2)
|
@@ -1,6 +1,49 @@
|
|
1
1
|
require 'spec_integration_helper'
|
2
2
|
|
3
3
|
describe RedSet do
|
4
|
+
describe "#initialize" do
|
5
|
+
it "creates a new RedSet instance and stores the key" do
|
6
|
+
set = RedSet.new("somekey")
|
7
|
+
|
8
|
+
set.key.should == "somekey"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#add" do
|
13
|
+
it "adds the given value to the set" do
|
14
|
+
set = RedSet.new("somekey")
|
15
|
+
|
16
|
+
set.add(1)
|
17
|
+
|
18
|
+
set.value.should == [1]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#value" do
|
23
|
+
it "returns the RedSet collection" do
|
24
|
+
set = RedSet.new("somekey")
|
25
|
+
set.value.should == []
|
26
|
+
|
27
|
+
set.add(1)
|
28
|
+
|
29
|
+
set.value.should == [1]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#remove" do
|
34
|
+
it "removes the item from the set" do
|
35
|
+
set = RedSet.new("somekey")
|
36
|
+
set.add(1)
|
37
|
+
set.add(2)
|
38
|
+
|
39
|
+
set.value.should == [2, 1]
|
40
|
+
|
41
|
+
set.remove(2)
|
42
|
+
|
43
|
+
set.value.should == [1]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
4
47
|
describe ".add" do
|
5
48
|
it "should add a member to the set" do
|
6
49
|
RedSet.add("somekey", 2)
|
@@ -31,7 +74,7 @@ describe RedSet do
|
|
31
74
|
it "sets the score when one is provided" do
|
32
75
|
RedSet.add("somekey", 1, :score => lambda { 1000 })
|
33
76
|
|
34
|
-
RedSet.score("somekey", 1).should ==
|
77
|
+
RedSet.score("somekey", 1).should == 1000
|
35
78
|
end
|
36
79
|
end
|
37
80
|
|
@@ -50,19 +93,33 @@ describe RedSet do
|
|
50
93
|
it "returns the score of member within key" do
|
51
94
|
RedSet.add("somekey", 1)
|
52
95
|
|
53
|
-
RedSet.score("somekey", 1).should ==
|
96
|
+
RedSet.score("somekey", 1).should == 0
|
54
97
|
end
|
55
98
|
end
|
56
99
|
|
57
100
|
describe ".get" do
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
101
|
+
it "should return the elements in reverse-inserted order" do
|
102
|
+
RedSet.add('somekey', 1)
|
103
|
+
RedSet.add('somekey', 2)
|
104
|
+
RedSet.add('somekey', 3)
|
105
|
+
|
106
|
+
RedSet.get('somekey').should == [3, 2, 1]
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should pair the elements with their scores if :with_scores is true" do
|
110
|
+
RedSet.add('somekey', 1)
|
111
|
+
RedSet.add('somekey', 2)
|
112
|
+
RedSet.add('somekey', 3)
|
113
|
+
|
114
|
+
RedSet.get('somekey', :with_scores => true).should == [[3, 2], [2, 1], [1, 0]]
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should return results bounded by provided pagination options" do
|
118
|
+
RedSet.add("somekey", 1)
|
119
|
+
RedSet.add("somekey", 2)
|
120
|
+
|
121
|
+
RedSet.get("somekey", :lower => 0, :upper => 0).should == [2]
|
122
|
+
RedSet.get("somekey", :lower => 1, :upper => 1).should == [1]
|
66
123
|
end
|
67
124
|
end
|
68
125
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redness
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -14,11 +14,11 @@ authors:
|
|
14
14
|
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
|
-
date: 2012-
|
17
|
+
date: 2012-08-03 00:00:00.000000000 Z
|
18
18
|
dependencies:
|
19
19
|
- !ruby/object:Gem::Dependency
|
20
20
|
name: json
|
21
|
-
requirement:
|
21
|
+
requirement: !ruby/object:Gem::Requirement
|
22
22
|
none: false
|
23
23
|
requirements:
|
24
24
|
- - ! '>='
|
@@ -26,10 +26,15 @@ dependencies:
|
|
26
26
|
version: '0'
|
27
27
|
type: :runtime
|
28
28
|
prerelease: false
|
29
|
-
version_requirements:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
30
35
|
- !ruby/object:Gem::Dependency
|
31
36
|
name: rspec
|
32
|
-
requirement:
|
37
|
+
requirement: !ruby/object:Gem::Requirement
|
33
38
|
none: false
|
34
39
|
requirements:
|
35
40
|
- - ! '>='
|
@@ -37,21 +42,31 @@ dependencies:
|
|
37
42
|
version: '0'
|
38
43
|
type: :runtime
|
39
44
|
prerelease: false
|
40
|
-
version_requirements:
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
41
51
|
- !ruby/object:Gem::Dependency
|
42
52
|
name: redis
|
43
|
-
requirement:
|
53
|
+
requirement: !ruby/object:Gem::Requirement
|
44
54
|
none: false
|
45
55
|
requirements:
|
46
56
|
- - ~>
|
47
57
|
- !ruby/object:Gem::Version
|
48
|
-
version:
|
58
|
+
version: 3.0.0
|
49
59
|
type: :runtime
|
50
60
|
prerelease: false
|
51
|
-
version_requirements:
|
61
|
+
version_requirements: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ~>
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 3.0.0
|
52
67
|
- !ruby/object:Gem::Dependency
|
53
68
|
name: activesupport
|
54
|
-
requirement:
|
69
|
+
requirement: !ruby/object:Gem::Requirement
|
55
70
|
none: false
|
56
71
|
requirements:
|
57
72
|
- - ! '>='
|
@@ -59,18 +74,44 @@ dependencies:
|
|
59
74
|
version: '0'
|
60
75
|
type: :runtime
|
61
76
|
prerelease: false
|
62
|
-
version_requirements:
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
63
83
|
- !ruby/object:Gem::Dependency
|
64
84
|
name: timecop
|
65
|
-
requirement:
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
66
86
|
none: false
|
67
87
|
requirements:
|
68
88
|
- - ! '>='
|
69
89
|
- !ruby/object:Gem::Version
|
70
90
|
version: '0'
|
71
|
-
type: :
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
- !ruby/object:Gem::Dependency
|
100
|
+
name: rake
|
101
|
+
requirement: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
type: :development
|
72
108
|
prerelease: false
|
73
|
-
version_requirements:
|
109
|
+
version_requirements: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
74
115
|
description: Helpful Ruby classes for improved Redis data structures
|
75
116
|
email: dev@howaboutwe.com
|
76
117
|
executables: []
|
@@ -78,8 +119,8 @@ extensions: []
|
|
78
119
|
extra_rdoc_files: []
|
79
120
|
files:
|
80
121
|
- .gitignore
|
122
|
+
- .rspec
|
81
123
|
- Gemfile
|
82
|
-
- Gemfile.lock
|
83
124
|
- LICENSE.txt
|
84
125
|
- RAKEFILE
|
85
126
|
- README.md
|
@@ -96,7 +137,7 @@ files:
|
|
96
137
|
- lib/redness/red_set_multi.rb
|
97
138
|
- lib/redness/red_set_union.rb
|
98
139
|
- lib/redness/timed_red_set.rb
|
99
|
-
- lib/version.rb
|
140
|
+
- lib/redness/version.rb
|
100
141
|
- redness.gemspec
|
101
142
|
- spec/redis_runner.rb
|
102
143
|
- spec/redness/red_capped_list_spec.rb
|
@@ -130,8 +171,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
171
|
version: '0'
|
131
172
|
requirements: []
|
132
173
|
rubyforge_project:
|
133
|
-
rubygems_version: 1.8.
|
174
|
+
rubygems_version: 1.8.24
|
134
175
|
signing_key:
|
135
176
|
specification_version: 3
|
136
177
|
summary: Helpful Ruby classes for improved Redis data structures
|
137
178
|
test_files: []
|
179
|
+
has_rdoc:
|
data/Gemfile.lock
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
activesupport (3.2.3)
|
5
|
-
i18n (~> 0.6)
|
6
|
-
multi_json (~> 1.0)
|
7
|
-
diff-lcs (1.1.3)
|
8
|
-
i18n (0.6.0)
|
9
|
-
json (1.6.6)
|
10
|
-
multi_json (1.3.2)
|
11
|
-
rake (0.9.2.2)
|
12
|
-
redis (2.2.2)
|
13
|
-
rspec (2.9.0)
|
14
|
-
rspec-core (~> 2.9.0)
|
15
|
-
rspec-expectations (~> 2.9.0)
|
16
|
-
rspec-mocks (~> 2.9.0)
|
17
|
-
rspec-core (2.9.0)
|
18
|
-
rspec-expectations (2.9.1)
|
19
|
-
diff-lcs (~> 1.1.3)
|
20
|
-
rspec-mocks (2.9.0)
|
21
|
-
timecop (0.3.5)
|
22
|
-
|
23
|
-
PLATFORMS
|
24
|
-
ruby
|
25
|
-
|
26
|
-
DEPENDENCIES
|
27
|
-
activesupport
|
28
|
-
json
|
29
|
-
rake
|
30
|
-
redis (~> 2.2.2)
|
31
|
-
rspec
|
32
|
-
timecop
|