redis-objective 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
1
+ Store objects in Redis
2
+
3
+ sudo gem install redis-objective
4
+
5
+ Usage
6
+ =====
7
+ require 'redis/objective'
8
+ redis = Redis.new(:host => ...).objective
9
+
10
+ redis['xxx'] = {:foo => :bar}
11
+ redis['xxx'] # {:foo => :bar}
12
+ redis['xxx'] = nil # delete key 'xxx'
13
+
14
+ # Non-objects can be read:
15
+ pure = Redis.new
16
+ pure['xxx'] = 'abc'
17
+ redis['xxx'] # 'abc'
18
+
19
+ # get/set - many
20
+ redis.mset('xxx' => {:foo => :bat}, 'yyy' => 'something else') # {'xxx' => {:foo => :bar}, 'yyy' => 'something else'}
21
+ redis.mget('xxx','yyy') # {'xxx' => {:foo => :bar}, 'yyy' => 'something else'}
22
+
23
+ ### Supported methods
24
+
25
+ - get / []
26
+ - set /set_with_expire / []=
27
+ - mget / mapped_mget
28
+ - mset / mapped_mset
29
+ - all other missing methods are delegated
30
+
31
+ TODO
32
+ ====
33
+ - also unset keys that were set to nil via mset
34
+
35
+ Author
36
+ ======
37
+ [Michael Grosser](http://pragmatig.wordpress.com)
38
+ grosser.michael@gmail.com
39
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
@@ -0,0 +1,20 @@
1
+ task :default => :spec
2
+ require 'spec/rake/spectask'
3
+ Spec::Rake::SpecTask.new {|t| t.spec_opts = ['--color']}
4
+
5
+ begin
6
+ require 'jeweler'
7
+ project_name = 'redis-objective'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = project_name
10
+ gem.summary = "Store objects in Redis"
11
+ gem.email = "grosser.michael@gmail.com"
12
+ gem.homepage = "http://github.com/grosser/#{project_name}"
13
+ gem.authors = ["Michael Grosser"]
14
+ gem.add_dependency ['redis']
15
+ end
16
+
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
20
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,59 @@
1
+ require 'redis'
2
+
3
+ class Redis::Client
4
+ def objective
5
+ @@objective ||= Redis::Objective.new(self)
6
+ end
7
+ end
8
+
9
+ class Redis::Objective
10
+ VERSION = File.read( File.join(File.dirname(__FILE__),'..','..','VERSION') ).strip
11
+
12
+ def initialize(client)
13
+ @client = client
14
+ end
15
+
16
+ def get(key, *args)
17
+ value = @client.get(key, *args)
18
+ return if value.nil?
19
+ YAML.load(value)
20
+ end
21
+ alias :[] :get
22
+
23
+ def set(key, value, *args)
24
+ if value.nil?
25
+ @client.del(key)
26
+ else
27
+ if args.empty?
28
+ @client.set(key, value.to_yaml)
29
+ else
30
+ @client.set_with_expire(key, value.to_yaml, *args)
31
+ end
32
+ end
33
+ value
34
+ end
35
+ alias :[]= :set
36
+ alias :set_with_expire :set
37
+
38
+ def mapped_mget(*args)
39
+ @client.mapped_mget(*args).inject({}){|h,k_v| h[k_v[0]] = YAML.load(k_v[1]); h}
40
+ end
41
+ alias :mget :mapped_mget
42
+
43
+ def mset(*args)
44
+ # flatten given hash/nested array
45
+ if args.size == 1
46
+ args = args.first.inject([]){|a,k_v| a << k_v[0]; a << k_v[1]; a}
47
+ end
48
+
49
+ # serialize all values [k1, v1, k2, v2, ...]
50
+ args.size.times{|i| args[i] = args[i].to_yaml if i % 2 == 1 }
51
+
52
+ @client.mset(*args)
53
+ end
54
+ alias :mapped_mset :mset
55
+
56
+ def method_missing(name, *args, &block)
57
+ @client.send(name, *args, &block)
58
+ end
59
+ end
@@ -0,0 +1,126 @@
1
+ require 'rubygems'
2
+
3
+ $LOAD_PATH << 'lib'
4
+ require 'redis/objective'
5
+
6
+ REDIS = Redis.new
7
+
8
+ REDIS['xxx'] = '1'
9
+ REDIS['xxx'].should == '1'
10
+
11
+ describe "Redis.objective" do
12
+ before do
13
+ REDIS.flushdb
14
+ end
15
+
16
+ describe :get do
17
+ before do
18
+ REDIS.objective.set('x',[1,2])
19
+ end
20
+
21
+ it 'works through []' do
22
+ REDIS.objective['x'].should == [1,2]
23
+ end
24
+
25
+ it 'works through get' do
26
+ REDIS.objective.get('x').should == [1,2]
27
+ end
28
+
29
+ it 'does crash with nil' do
30
+ REDIS.objective.get('unknown').should == nil
31
+ end
32
+ end
33
+
34
+ describe :set do
35
+ it 'works through []' do
36
+ REDIS.objective['x'] = [1,2]
37
+ REDIS.objective['x'].should == [1,2]
38
+ end
39
+
40
+ it 'works through set' do
41
+ REDIS.objective.set('x', [1,2])
42
+ REDIS.objective['x'].should == [1,2]
43
+ end
44
+
45
+ it 'works through set_with_expire' do
46
+ pending 'my local redis server des not support this...'
47
+ REDIS.objective.set_with_expire('x', [1,2], 1)
48
+ REDIS.objective['x'].should == [1,2]
49
+ sleep 1
50
+ REDIS.objective['x'].should == nil
51
+ end
52
+
53
+ it 'does not set nil' do
54
+ REDIS.objective['x'] = nil
55
+ REDIS['x'].should == nil
56
+ end
57
+
58
+ it 'overwrites when setting nil' do
59
+ REDIS.objective['x'] = [1,2]
60
+ REDIS.objective['x'] = nil
61
+ REDIS['x'].should == nil
62
+ end
63
+
64
+ it 'returns the set value' do
65
+ REDIS.objective.set('x',[1,2]).should == [1,2]
66
+ REDIS.objective.set('x',nil).should == nil
67
+ end
68
+ end
69
+
70
+ describe :mapped_mget do
71
+ before do
72
+ REDIS.objective['xx'] = [1,2]
73
+ REDIS.objective['xy'] = [3,4]
74
+ end
75
+
76
+ it 'gets objective' do
77
+ REDIS.objective.mapped_mget('xx','xy').should == {'xx' => [1,2], 'xy' => [3,4]}
78
+ end
79
+
80
+ it 'does not crash with nil' do
81
+ REDIS.objective.mapped_mget('xx','unknown').should == {'xx' => [1,2]}
82
+ end
83
+
84
+ it 'works with mget' do
85
+ REDIS.objective.mget('xx','unknown').should == {'xx' => [1,2]}
86
+ end
87
+ end
88
+
89
+ describe :mapped_mset do
90
+ it 'sets objective' do
91
+ REDIS.objective.mapped_mset('xx' => [1,2], 'xy' => [3,4])
92
+ REDIS.objective['xx'].should == [1,2]
93
+ REDIS.objective['xy'].should == [3,4]
94
+ end
95
+
96
+ it 'sets with nested array' do
97
+ REDIS.objective.mapped_mset([['xx', [1,2]], ['xy', [3,4]]])
98
+ REDIS.objective['xx'].should == [1,2]
99
+ REDIS.objective['xy'].should == [3,4]
100
+ end
101
+
102
+ it 'sets with flat array' do
103
+ REDIS.objective.mapped_mset('xx', [1,2], 'xy', [3,4])
104
+ REDIS.objective['xx'].should == [1,2]
105
+ REDIS.objective['xy'].should == [3,4]
106
+ end
107
+
108
+ it 'sets with mset' do
109
+ REDIS.objective.mset('xx', [1,2], 'xy', [3,4])
110
+ REDIS.objective['xx'].should == [1,2]
111
+ REDIS.objective['xy'].should == [3,4]
112
+ end
113
+
114
+ it 'can set nil' do
115
+ REDIS.objective.mset('xx', [1,2], 'xy', nil)
116
+ REDIS.objective['xx'].should == [1,2]
117
+ REDIS.objective['xy'].should == nil
118
+ end
119
+
120
+ it 'deletes keys that are set to nil'
121
+ end
122
+
123
+ it 'has a version' do
124
+ Redis::Objective::VERSION.should =~ /^\d+\.\d+\.\d+$/
125
+ end
126
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis-objective
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Michael Grosser
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-13 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: redis
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ description:
33
+ email: grosser.michael@gmail.com
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - README.md
40
+ files:
41
+ - README.md
42
+ - Rakefile
43
+ - VERSION
44
+ - lib/redis/objective.rb
45
+ - spec/redis/objective_spec.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/grosser/redis-objective
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.6
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Store objects in Redis
76
+ test_files:
77
+ - spec/redis/objective_spec.rb