redis-classy 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -0
- data/lib/redis/classy/version.rb +1 -1
- data/lib/redis/classy.rb +14 -9
- data/spec/redis_classy_spec.rb +34 -27
- metadata +32 -11
data/README.md
CHANGED
@@ -22,6 +22,9 @@ This library contains only 30+ lines of code, yet powerful when you need better
|
|
22
22
|
|
23
23
|
### What's new:
|
24
24
|
|
25
|
+
* v1.2.0: Raise NoMethodError when commands are not found in redis-rb.
|
26
|
+
* v1.1.1: Raise exception when Redis::Classy.db is not assigned
|
27
|
+
* v1.1.0: Explicitly require all files
|
25
28
|
* v1.0.1: Relaxed version dependency on redis-namespace
|
26
29
|
* v1.0.0: Play nice with Mongoid
|
27
30
|
|
@@ -152,6 +155,19 @@ Since the `db` attribute is a class instance variable, you can dynamically assig
|
|
152
155
|
UniqueUser.db = Redis::Namespace.new('UniqueUser', :redis => Redis.new(:host => 'another.host'))
|
153
156
|
```
|
154
157
|
|
158
|
+
Unicorn support
|
159
|
+
---------------
|
160
|
+
|
161
|
+
If you run fork-based app servers such as **Unicorn** or **Passenger**, you need to reconnect to the Redis after forking.
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
after_fork do
|
165
|
+
Redis::Classy.db.client.reconnect
|
166
|
+
end
|
167
|
+
```
|
168
|
+
|
169
|
+
Note that since Redis Classy assigns a namespaced Redis instance upon the inheritance event of each subclass (`class Something < Redis::Classy`), reconnecting the master (non-namespaced) connection that is referenced from all subclasses should probably be the safest and the most efficient way to survive a forking event.
|
170
|
+
|
155
171
|
Reference
|
156
172
|
---------
|
157
173
|
|
data/lib/redis/classy/version.rb
CHANGED
data/lib/redis/classy.rb
CHANGED
@@ -4,17 +4,22 @@ class Redis
|
|
4
4
|
attr_accessor :db
|
5
5
|
|
6
6
|
def inherited(subclass)
|
7
|
-
|
7
|
+
raise 'Redis::Classy.db is not assigned' unless Redis::Classy.db
|
8
|
+
subclass.db = Redis::Namespace.new(subclass.name, :redis => Redis::Classy.db)
|
8
9
|
end
|
9
10
|
|
10
|
-
def
|
11
|
-
db.
|
11
|
+
def delegatables
|
12
|
+
@delegatables ||= Redis::Classy.db.class.instance_methods(false).map(&:to_sym) # ruby1.8 returns strings
|
12
13
|
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
def method_missing(command, *args, &block)
|
16
|
+
return super unless delegatables.include?(command)
|
17
|
+
db.send(command, *args, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
Redis::Namespace::COMMANDS.keys.each do |command|
|
21
|
+
define_method(command) do |*args, &block|
|
22
|
+
db.send(command, *args, &block)
|
18
23
|
end
|
19
24
|
end
|
20
25
|
end
|
@@ -25,8 +30,8 @@ class Redis
|
|
25
30
|
@key = key
|
26
31
|
end
|
27
32
|
|
28
|
-
def method_missing(
|
29
|
-
self.class.send(
|
33
|
+
def method_missing(command, *args, &block)
|
34
|
+
self.class.send(command, @key, *args, &block)
|
30
35
|
end
|
31
36
|
end
|
32
37
|
end
|
data/spec/redis_classy_spec.rb
CHANGED
@@ -11,49 +11,47 @@ module Deep
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
describe
|
14
|
+
describe Redis::Classy do
|
15
15
|
before do
|
16
16
|
Redis::Classy.flushdb
|
17
17
|
end
|
18
18
|
|
19
|
-
after do
|
20
|
-
Redis::Classy.flushdb
|
21
|
-
end
|
22
|
-
|
23
19
|
after(:all) do
|
20
|
+
Redis::Classy.flushdb
|
24
21
|
Redis::Classy.quit
|
25
22
|
end
|
26
23
|
|
27
|
-
it
|
28
|
-
Something.set(
|
29
|
-
Something.keys.should == [
|
30
|
-
Redis::Classy.keys.should include
|
24
|
+
it 'prepends class name to the key' do
|
25
|
+
Something.set('foo', 'bar')
|
26
|
+
Something.keys.should == ['foo']
|
27
|
+
Redis::Classy.keys.should include 'Something:foo'
|
31
28
|
|
32
|
-
Another.set(
|
33
|
-
Another.keys.should == [
|
34
|
-
Redis::Classy.keys.should include
|
29
|
+
Another.set('foo', 'bar')
|
30
|
+
Another.keys.should == ['foo']
|
31
|
+
Redis::Classy.keys.should include 'Another:foo'
|
35
32
|
|
36
|
-
Deep::Klass.set(
|
37
|
-
Deep::Klass.keys.should == [
|
38
|
-
Redis::Classy.keys.should include
|
33
|
+
Deep::Klass.set('foo', 'bar')
|
34
|
+
Deep::Klass.keys.should == ['foo']
|
35
|
+
Redis::Classy.keys.should include 'Deep::Klass:foo'
|
39
36
|
end
|
40
37
|
|
41
|
-
it
|
42
|
-
Something.get(
|
43
|
-
Something.set(
|
44
|
-
Something.get(
|
38
|
+
it 'delegates to class methods' do
|
39
|
+
Something.get('foo').should == nil
|
40
|
+
Something.set('foo', 'bar')
|
41
|
+
Something.get('foo').should == 'bar'
|
45
42
|
end
|
46
43
|
|
47
|
-
it
|
48
|
-
something = Something.new(
|
44
|
+
it 'delegates instance methods with the key binding' do
|
45
|
+
something = Something.new('foo')
|
49
46
|
|
50
47
|
something.get.should == nil
|
51
|
-
something.set(
|
52
|
-
something.get.should ==
|
48
|
+
something.set('bar')
|
49
|
+
something.get.should == 'bar'
|
50
|
+
Something.get('foo').should == 'bar'
|
53
51
|
end
|
54
52
|
|
55
|
-
it
|
56
|
-
something = Something.new(
|
53
|
+
it 'handles multi block' do
|
54
|
+
something = Something.new('foo')
|
57
55
|
|
58
56
|
Redis::Classy.multi do
|
59
57
|
something.sadd 1
|
@@ -64,7 +62,16 @@ describe "RedisClassy" do
|
|
64
62
|
something.smembers.should == ['1','2','3']
|
65
63
|
end
|
66
64
|
|
67
|
-
it
|
65
|
+
it 'handles method_missing' do
|
66
|
+
# class method
|
67
|
+
expect { Something.bogus }.to raise_error(NoMethodError)
|
68
|
+
|
69
|
+
# instance method
|
70
|
+
something = Something.new('foo')
|
71
|
+
expect { something.bogus }.to raise_error(NoMethodError)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should battle against mongoid' do
|
68
75
|
# Emulate notorious Mongoid::Extensions::Object::Conversions
|
69
76
|
class Object
|
70
77
|
def self.get(value)
|
@@ -74,7 +81,7 @@ describe "RedisClassy" do
|
|
74
81
|
|
75
82
|
# This would have returned "foo" instead of nil, unless we explicitly defined
|
76
83
|
# class methods from Redis::Namespace::COMMANDS
|
77
|
-
Something.get(
|
84
|
+
Something.get('foo').should == nil
|
78
85
|
|
79
86
|
class << Object
|
80
87
|
remove_method :get
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis-classy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis-namespace
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '1.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: rspec
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,10 +37,15 @@ dependencies:
|
|
32
37
|
version: '0'
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: bundler
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ! '>='
|
@@ -43,10 +53,15 @@ dependencies:
|
|
43
53
|
version: '0'
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: rake
|
49
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
50
65
|
none: false
|
51
66
|
requirements:
|
52
67
|
- - ! '>='
|
@@ -54,7 +69,12 @@ dependencies:
|
|
54
69
|
version: '0'
|
55
70
|
type: :development
|
56
71
|
prerelease: false
|
57
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
58
78
|
description: Class-style namespace prefixing for Redis
|
59
79
|
email:
|
60
80
|
- kenn.ejima@gmail.com
|
@@ -95,10 +115,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
115
|
version: '0'
|
96
116
|
requirements: []
|
97
117
|
rubyforge_project:
|
98
|
-
rubygems_version: 1.8.
|
118
|
+
rubygems_version: 1.8.19
|
99
119
|
signing_key:
|
100
120
|
specification_version: 3
|
101
121
|
summary: Class-style namespace prefixing for Redis
|
102
122
|
test_files:
|
103
123
|
- spec/redis_classy_spec.rb
|
104
124
|
- spec/spec_helper.rb
|
125
|
+
has_rdoc:
|