redis-classy 0.9.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,9 +1,9 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem "redis-namespace", "~> 0.10.0"
3
+ gem "redis-namespace", "~> 1.0.0"
4
4
 
5
5
  group :development do
6
- gem "rspec", "~> 2.5.0"
6
+ gem "rspec", "~> 2.6.0"
7
7
  gem "bundler", "~> 1.0.0"
8
- gem "jeweler", "~> 1.5.2"
8
+ gem "jeweler", "~> 1.6.0"
9
9
  end
data/Gemfile.lock CHANGED
@@ -3,28 +3,28 @@ GEM
3
3
  specs:
4
4
  diff-lcs (1.1.2)
5
5
  git (1.2.5)
6
- jeweler (1.5.2)
6
+ jeweler (1.6.0)
7
7
  bundler (~> 1.0.0)
8
8
  git (>= 1.2.5)
9
9
  rake
10
- rake (0.8.7)
11
- redis (2.1.1)
12
- redis-namespace (0.10.0)
10
+ rake (0.9.0)
11
+ redis (2.2.0)
12
+ redis-namespace (1.0.3)
13
13
  redis (< 3.0.0)
14
- rspec (2.5.0)
15
- rspec-core (~> 2.5.0)
16
- rspec-expectations (~> 2.5.0)
17
- rspec-mocks (~> 2.5.0)
18
- rspec-core (2.5.1)
19
- rspec-expectations (2.5.0)
14
+ rspec (2.6.0)
15
+ rspec-core (~> 2.6.0)
16
+ rspec-expectations (~> 2.6.0)
17
+ rspec-mocks (~> 2.6.0)
18
+ rspec-core (2.6.2)
19
+ rspec-expectations (2.6.0)
20
20
  diff-lcs (~> 1.1.2)
21
- rspec-mocks (2.5.0)
21
+ rspec-mocks (2.6.0)
22
22
 
23
23
  PLATFORMS
24
24
  ruby
25
25
 
26
26
  DEPENDENCIES
27
27
  bundler (~> 1.0.0)
28
- jeweler (~> 1.5.2)
29
- redis-namespace (~> 0.10.0)
30
- rspec (~> 2.5.0)
28
+ jeweler (~> 1.6.0)
29
+ redis-namespace (~> 1.0.0)
30
+ rspec (~> 2.6.0)
data/README.md ADDED
@@ -0,0 +1,129 @@
1
+ Redis Classy
2
+ ============
3
+
4
+ Class-style namespace prefixing for Redis.
5
+
6
+ With Redis Classy, class names become the prefix part of the Redis keys.
7
+
8
+ class Something < Redis::Classy
9
+ end
10
+
11
+ Something.set "foo", "bar" # equivalent of => redis.set "Something:foo", "bar"
12
+ Something.get "foo" # => redis.get "Something:foo"
13
+ => "bar"
14
+
15
+ This library contains only 33 lines of code, yet powerful when you need better abstraction on Redis objects to keep things organized.
16
+
17
+ Requies the `redis-namespace` gem.
18
+
19
+ What's new:
20
+
21
+ * v1.0.0: Play nice with Mongoid
22
+
23
+ Synopsis
24
+ --------
25
+
26
+ With the vanilla redis gem, you've been doing this:
27
+
28
+ redis = Redis.new
29
+ redis.set "foo", "bar"
30
+
31
+ With the redis-namespace gem, you can add a prefix in the following manner:
32
+
33
+ redis_ns = Redis::Namespace.new('ns', :redis => redis)
34
+ redis_ns['foo'] = 'bar' # equivalent of => redis.set "ns:foo", "bar"
35
+
36
+ Now, with the redis-classy gem, you could finally do:
37
+
38
+ class Something < Redis::Classy
39
+ end
40
+
41
+ Something.set "foo", "bar" # equivalent of => redis.set "Prefix:foo", "bar"
42
+ Something.get "foo"
43
+ => "bar"
44
+
45
+ Install
46
+ -------
47
+
48
+ gem install redis-classy
49
+
50
+ Usage
51
+ -----
52
+
53
+ In Gemfile:
54
+
55
+ gem "redis-classy"
56
+
57
+ In config/initializers/redis_classy.rb:
58
+
59
+ Redis::Classy.db = Redis.new
60
+
61
+ Now you can write models that inherit the Redis::Classy class, automatically prefixing keys with its class name.
62
+ You can use any Redis commands on the class, since they are simply passed to the Redis instance.
63
+
64
+ class UniqueUser < Redis::Classy
65
+ def self.nuke
66
+ self.keys.each{|key| self.del(key) }
67
+ end
68
+ end
69
+
70
+ UniqueUser.sadd "2011-02-28", @user_a.id
71
+ UniqueUser.sadd "2011-02-28", @user_b.id
72
+ UniqueUser.sadd "2011-03-01", @user_c.id
73
+
74
+ UniqueUser.smembers "2011-02-28"
75
+ => ["123", "456"]
76
+
77
+ UniqueUser.nuke
78
+ => ["2011-02-28", "2011-03-01"]
79
+
80
+ UniqueUser.keys
81
+ => []
82
+
83
+ In most cases you may be just fine with class methods, but by creating an instance with a key, even further binding is possible.
84
+
85
+ class Counter < Redis::Classy
86
+ def initialize(object)
87
+ super("#{object.class.name}:#{object.id}")
88
+ end
89
+ end
90
+
91
+ class Room < ActiveRecord::Base
92
+ end
93
+
94
+ @room = Room.create
95
+
96
+ counter = Counter.new(@room)
97
+ counter.incr
98
+ counter.incr
99
+ counter.get
100
+ => "2"
101
+
102
+ counter.key
103
+ => "Room:123"
104
+
105
+ You also have access to the non-namespaced, raw Redis instance via Redis::Classy
106
+
107
+ Redis::Classy.keys "UniqueUser:*"
108
+ => ["UniqueUser:2011-02-28", "UniqueUser:2011-03-01"]
109
+
110
+ Redis::Classy.multi do
111
+ UniqueUser.sadd "2011-02-28", @user_a.id
112
+ UniqueUser.sadd "2011-02-28", @user_b.id
113
+ end
114
+
115
+ Since the "db" attribute is a class instance variable, you can dynamically assign different databases for each class.
116
+
117
+ UniqueUser.db = Redis::Namespace.new("UniqueUser", :redis => Redis.new(:host => "another.host"))
118
+
119
+ Reference
120
+ ---------
121
+
122
+ Dependency:
123
+
124
+ * <https://github.com/ezmobius/redis-rb>
125
+ * <https://github.com/defunkt/redis-namespace>
126
+
127
+ Use case:
128
+
129
+ * <https://github.com/kenn/redis-mutex>
data/Rakefile CHANGED
@@ -7,39 +7,22 @@ rescue Bundler::BundlerError => e
7
7
  $stderr.puts "Run `bundle install` to install missing gems"
8
8
  exit e.status_code
9
9
  end
10
- require 'rake'
10
+ require 'rake/file_utils'
11
+ include FileUtils
11
12
 
12
13
  require 'jeweler'
13
14
  Jeweler::Tasks.new do |gem|
14
15
  gem.name = "redis-classy"
15
16
  gem.homepage = "http://github.com/kenn/redis-classy"
16
17
  gem.license = "MIT"
17
- gem.summary = "Model-friendly base class for Redis"
18
- gem.description = "Make the prefix part of the Redis keys a class name for your model."
18
+ gem.summary = "Class-style namespace prefixing for Redis"
19
+ gem.description = "Class-style namespace prefixing for Redis"
19
20
  gem.email = "kenn.ejima@gmail.com"
20
21
  gem.authors = ["Kenn Ejima"]
21
22
  end
22
23
  Jeweler::RubygemsDotOrgTasks.new
23
24
 
24
- require 'rspec/core'
25
- require 'rspec/core/rake_task'
26
- RSpec::Core::RakeTask.new(:spec) do |spec|
27
- spec.pattern = FileList['spec/**/*_spec.rb']
28
- end
29
-
30
- RSpec::Core::RakeTask.new(:rcov) do |spec|
31
- spec.pattern = 'spec/**/*_spec.rb'
32
- spec.rcov = true
33
- end
34
-
35
25
  task :default => :spec
36
-
37
- require 'rake/rdoctask'
38
- Rake::RDocTask.new do |rdoc|
39
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
-
41
- rdoc.rdoc_dir = 'rdoc'
42
- rdoc.title = "redis-classy #{version}"
43
- rdoc.rdoc_files.include('README*')
44
- rdoc.rdoc_files.include('lib/**/*.rb')
26
+ task :spec do
27
+ exec "rspec spec/redis-classy_spec.rb"
45
28
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.1
1
+ 1.0.0
data/lib/redis/classy.rb CHANGED
@@ -12,6 +12,12 @@ class Redis
12
12
  def method_missing(method_name, *args, &block)
13
13
  self.db.send(method_name, *args, &block)
14
14
  end
15
+
16
+ Redis::Namespace::COMMANDS.keys.each do |key|
17
+ define_method(key) do |*args|
18
+ self.db.send(key, *args)
19
+ end
20
+ end
15
21
  end
16
22
 
17
23
  attr_accessor :key
data/redis-classy.gemspec CHANGED
@@ -5,16 +5,16 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{redis-classy}
8
- s.version = "0.9.1"
8
+ s.version = "1.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kenn Ejima"]
12
- s.date = %q{2011-03-16}
13
- s.description = %q{Make the prefix part of the Redis keys a class name for your model.}
12
+ s.date = %q{2011-05-21}
13
+ s.description = %q{Class-style namespace prefixing for Redis}
14
14
  s.email = %q{kenn.ejima@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
- "README.rdoc"
17
+ "README.md"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
22
22
  "Gemfile",
23
23
  "Gemfile.lock",
24
24
  "LICENSE.txt",
25
- "README.rdoc",
25
+ "README.md",
26
26
  "Rakefile",
27
27
  "VERSION",
28
28
  "lib/redis-classy.rb",
@@ -34,32 +34,28 @@ Gem::Specification.new do |s|
34
34
  s.homepage = %q{http://github.com/kenn/redis-classy}
35
35
  s.licenses = ["MIT"]
36
36
  s.require_paths = ["lib"]
37
- s.rubygems_version = %q{1.5.3}
38
- s.summary = %q{Model-friendly base class for Redis}
39
- s.test_files = [
40
- "spec/redis-classy_spec.rb",
41
- "spec/spec_helper.rb"
42
- ]
37
+ s.rubygems_version = %q{1.6.2}
38
+ s.summary = %q{Class-style namespace prefixing for Redis}
43
39
 
44
40
  if s.respond_to? :specification_version then
45
41
  s.specification_version = 3
46
42
 
47
43
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
- s.add_runtime_dependency(%q<redis-namespace>, ["~> 0.10.0"])
49
- s.add_development_dependency(%q<rspec>, ["~> 2.5.0"])
44
+ s.add_runtime_dependency(%q<redis-namespace>, ["~> 1.0.0"])
45
+ s.add_development_dependency(%q<rspec>, ["~> 2.6.0"])
50
46
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
51
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
47
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
52
48
  else
53
- s.add_dependency(%q<redis-namespace>, ["~> 0.10.0"])
54
- s.add_dependency(%q<rspec>, ["~> 2.5.0"])
49
+ s.add_dependency(%q<redis-namespace>, ["~> 1.0.0"])
50
+ s.add_dependency(%q<rspec>, ["~> 2.6.0"])
55
51
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
56
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
57
53
  end
58
54
  else
59
- s.add_dependency(%q<redis-namespace>, ["~> 0.10.0"])
60
- s.add_dependency(%q<rspec>, ["~> 2.5.0"])
55
+ s.add_dependency(%q<redis-namespace>, ["~> 1.0.0"])
56
+ s.add_dependency(%q<rspec>, ["~> 2.6.0"])
61
57
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
62
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
63
59
  end
64
60
  end
65
61
 
@@ -1,7 +1,65 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
+ class Something < Redis::Classy
4
+ end
5
+
3
6
  describe "RedisClassy" do
4
- pending "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
7
+ before(:each) do
8
+ Redis::Classy.flushdb
9
+ end
10
+
11
+ after(:each) do
12
+ Redis::Classy.flushdb
13
+ end
14
+
15
+ after(:all) do
16
+ Redis::Classy.quit
17
+ end
18
+
19
+ it "should prepend class name to the key" do
20
+ Something.set("foo", "bar")
21
+
22
+ Something.keys.should == ["foo"]
23
+ Redis::Classy.keys.should == ["Something:foo"]
24
+ end
25
+
26
+ it "should delegate class methods" do
27
+ Something.get("foo").should == nil
28
+ Something.set("foo", "bar")
29
+ Something.get("foo").should == "bar"
30
+ end
31
+
32
+ it "should delegate instance methods with the key binding" do
33
+ something = Something.new("foo")
34
+
35
+ something.get.should == nil
36
+ something.set("bar")
37
+ something.get.should == "bar"
38
+ end
39
+
40
+ it "should handle multi block" do
41
+ Redis::Classy.multi do
42
+ Something.sadd "foo", "bar"
43
+ Something.sadd "foo", "baz"
44
+ end
45
+
46
+ Something.smembers("foo").should == ["baz", "bar"]
47
+ end
48
+
49
+ it "should battle against mongoid" do
50
+ # Emulate notorious Mongoid::Extensions::Object::Conversions
51
+ class Object
52
+ def self.get(value)
53
+ value
54
+ end
55
+ end
56
+
57
+ # This would have returned "foo" instead of nil, unless we explicitly defined
58
+ # class methods from Redis::Namespace::COMMANDS
59
+ Something.get("foo").should == nil
60
+
61
+ class << Object
62
+ remove_method :get
63
+ end
6
64
  end
7
65
  end
data/spec/spec_helper.rb CHANGED
@@ -8,5 +8,6 @@ require 'redis-classy'
8
8
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
9
 
10
10
  RSpec.configure do |config|
11
-
11
+ # Use database 15 for testing so we don't accidentally step on you real data.
12
+ Redis::Classy.db = Redis.new(:db => 15)
12
13
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: redis-classy
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.9.1
5
+ version: 1.0.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kenn Ejima
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-16 00:00:00 -07:00
13
+ date: 2011-05-21 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ~>
22
22
  - !ruby/object:Gem::Version
23
- version: 0.10.0
23
+ version: 1.0.0
24
24
  type: :runtime
25
25
  prerelease: false
26
26
  version_requirements: *id001
@@ -31,7 +31,7 @@ dependencies:
31
31
  requirements:
32
32
  - - ~>
33
33
  - !ruby/object:Gem::Version
34
- version: 2.5.0
34
+ version: 2.6.0
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: *id002
@@ -53,11 +53,11 @@ dependencies:
53
53
  requirements:
54
54
  - - ~>
55
55
  - !ruby/object:Gem::Version
56
- version: 1.5.2
56
+ version: 1.6.0
57
57
  type: :development
58
58
  prerelease: false
59
59
  version_requirements: *id004
60
- description: Make the prefix part of the Redis keys a class name for your model.
60
+ description: Class-style namespace prefixing for Redis
61
61
  email: kenn.ejima@gmail.com
62
62
  executables: []
63
63
 
@@ -65,14 +65,14 @@ extensions: []
65
65
 
66
66
  extra_rdoc_files:
67
67
  - LICENSE.txt
68
- - README.rdoc
68
+ - README.md
69
69
  files:
70
70
  - .document
71
71
  - .rspec
72
72
  - Gemfile
73
73
  - Gemfile.lock
74
74
  - LICENSE.txt
75
- - README.rdoc
75
+ - README.md
76
76
  - Rakefile
77
77
  - VERSION
78
78
  - lib/redis-classy.rb
@@ -94,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - ">="
96
96
  - !ruby/object:Gem::Version
97
- hash: -2882799030924789426
97
+ hash: 1700316394967053203
98
98
  segments:
99
99
  - 0
100
100
  version: "0"
@@ -107,10 +107,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  requirements: []
108
108
 
109
109
  rubyforge_project:
110
- rubygems_version: 1.5.3
110
+ rubygems_version: 1.6.2
111
111
  signing_key:
112
112
  specification_version: 3
113
- summary: Model-friendly base class for Redis
114
- test_files:
115
- - spec/redis-classy_spec.rb
116
- - spec/spec_helper.rb
113
+ summary: Class-style namespace prefixing for Redis
114
+ test_files: []
115
+
data/README.rdoc DELETED
@@ -1,106 +0,0 @@
1
- = Redis Classy
2
-
3
- Model-friendly base class for Redis.
4
-
5
- With this library, the class name of your model becomes the prefix part of the Redis keys.
6
-
7
- This library contains only 27 lines of code, yet powerful when you need more abstraction on Redis objects rather than dealing with complicated keys in all strings.
8
-
9
- Requies the redis-namespace gem.
10
-
11
- == Synopsis
12
-
13
- With the vanilla redis gem, you've been normally doing this:
14
-
15
- redis = Redis.new
16
- redis.set "foo", "bar"
17
-
18
- With the redis-namespace gem, you could add a prefix in the following manner:
19
-
20
- redis_ns = Redis::Namespace.new('ns', :redis => redis)
21
- redis_ns['foo'] = 'bar' # equivalent of => redis.set "ns:foo", "bar"
22
-
23
- Now, with the redis-classy gem, you could finally do:
24
-
25
- class Prefix < Redis::Classy
26
- end
27
-
28
- Prefix.set "foo", "bar" # equivalent of => redis.set "Prefix:foo", "bar"
29
- Prefix.get "foo"
30
- => "bar"
31
-
32
- == Install
33
-
34
- gem install redis-classy
35
-
36
- == Usage
37
-
38
- In Gemfile:
39
-
40
- gem "redis-classy"
41
-
42
- In config/initializers/redis_classy.rb:
43
-
44
- Redis::Classy.db = Redis.new
45
-
46
- Now you can write models that inherit the Redis::Classy class, automatically prefixing keys with its class name.
47
- You can use any Redis commands on the class, since they are simply passed to the Redis instance.
48
-
49
- class UniqueUser < Redis::Classy
50
- def self.nuke
51
- self.keys.each{|key| self.del(key) }
52
- end
53
- end
54
-
55
- UniqueUser.sadd "2011-02-28", @user_a.id
56
- UniqueUser.sadd "2011-02-28", @user_b.id
57
- UniqueUser.sadd "2011-03-01", @user_c.id
58
-
59
- UniqueUser.smembers "2011-02-28"
60
- => ["123", "456"]
61
-
62
- UniqueUser.nuke
63
- => ["2011-02-28", "2011-03-01"]
64
-
65
- UniqueUser.keys
66
- => []
67
-
68
- In most cases you may be just fine with class methods, but by creating an instance with a key, even further binding is possible.
69
-
70
- class Counter < Redis::Classy
71
- def initialize(object)
72
- super("#{object.class.name}:#{object.id}")
73
- end
74
- end
75
-
76
- counter = Counter.new(@room)
77
- counter.incr
78
- counter.incr
79
- counter.get
80
- => "2"
81
-
82
- counter.key
83
- => "Room:123"
84
-
85
- You also have access to the non-namespaced, raw Redis instance via Redis::Classy
86
-
87
- Redis::Classy.keys "UniqueUser:*"
88
- => ["UniqueUser:2011-02-28", "UniqueUser:2011-03-01"]
89
-
90
- Redis::Classy.multi do
91
- UniqueUser.sadd "2011-02-28", @user_a.id
92
- UniqueUser.sadd "2011-02-28", @user_b.id
93
- end
94
-
95
- Since the "db" attribute is a class instance variable, you can dynamically assign different databases for each class.
96
-
97
- UniqueUser.db = Redis::Namespace.new("UniqueUser", :redis => Redis.new(:host => "another.host"))
98
-
99
- == Reference
100
-
101
- Dependency:
102
- https://github.com/ezmobius/redis-rb
103
- https://github.com/defunkt/redis-namespace
104
-
105
- Use case:
106
- https://github.com/kenn/redis-mutex