redis_backed_model 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,15 @@
1
1
  module RedisBackedModel
2
2
  class RedisBackedModel
3
3
 
4
+ def self.find(*args)
5
+ found = []
6
+ args.flatten.each do |id|
7
+ attributes = $redis.hgetall("#{self.to_s.underscore}:#{id}")
8
+ found << self.new(attributes.merge({'id' => id})) if attributes.size > 0
9
+ end
10
+ (found.count == 1) ? found.first : found
11
+ end
12
+
4
13
  def initialize(attributes={})
5
14
  if attributes.class == Hash
6
15
  attributes.each do |key, value|
@@ -1,3 +1,3 @@
1
1
  module RedisBackedModel
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -3,7 +3,7 @@ require 'extras/person'
3
3
 
4
4
  describe RedisBackedModel do
5
5
  before(:all) do
6
-
6
+ class InheritingFromRedisBackedModel < RedisBackedModel::RedisBackedModel; end
7
7
  end
8
8
 
9
9
  before(:each) do
@@ -19,18 +19,18 @@ describe RedisBackedModel do
19
19
  end
20
20
 
21
21
  it "has no instance variables unless specified" do
22
- rbm = RedisBackedModel::RedisBackedModel.new
22
+ rbm = InheritingFromRedisBackedModel.new
23
23
  rbm.instance_variables.count.should eq(0)
24
24
  end
25
25
 
26
26
  it "creates an instance variable for a hash with one member" do
27
27
  attributes = {'id' => 1}
28
- rbm = RedisBackedModel::RedisBackedModel.new(attributes)
28
+ rbm = InheritingFromRedisBackedModel.new(attributes)
29
29
  rbm.instance_variables.include?(:@id).should eq(true)
30
30
  end
31
31
 
32
32
  it "creates an instance variable for each member of an attribute hash that doesn't have scores" do
33
- rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
33
+ rbm = InheritingFromRedisBackedModel.new(@attributes_with_id)
34
34
  rbm.instance_variables.count.should eq(@size)
35
35
  @attributes_with_id.each do | key, value|
36
36
  # rbm.instance_variables.include?("@#{key}".to_sym).should eq(true), "no instance variable for #{key}"
@@ -39,8 +39,8 @@ describe RedisBackedModel do
39
39
  end
40
40
 
41
41
  it "raises an argument error if something other than a hash is passed in" do
42
- expect { RedisBackedModel::RedisBackedModel.new('w') }.to raise_error(ArgumentError)
43
- expect { RedisBackedModel::RedisBackedModel.new(['w', 1]) }.to raise_error(ArgumentError)
42
+ expect { InheritingFromRedisBackedModel.new('w') }.to raise_error(ArgumentError)
43
+ expect { InheritingFromRedisBackedModel.new(['w', 1]) }.to raise_error(ArgumentError)
44
44
  end
45
45
 
46
46
  context "initializing with the attribute hash contains a score_[|] key" do
@@ -49,12 +49,12 @@ describe RedisBackedModel do
49
49
  @attributes_with_id[@score_key] = '[1|2]'
50
50
  end
51
51
  it "does not create a key for any score_[|] attribute" do
52
- rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
52
+ rbm = InheritingFromRedisBackedModel.new(@attributes_with_id)
53
53
  rbm.instance_variables.include?(@score_key.instance_variableize).should eq(false)
54
54
  end
55
55
 
56
56
  it "creates a scores instance variable if there are any score_[x|y] attributes" do
57
- rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
57
+ rbm = InheritingFromRedisBackedModel.new(@attributes_with_id)
58
58
  rbm.instance_variables.include?('scores'.instance_variableize).should eq(true)
59
59
  end
60
60
  end
@@ -63,7 +63,7 @@ describe RedisBackedModel do
63
63
  ['score_[foo|bar]', 'score_[baz|qux]', 'score_[wibble|wobble]'].each_with_index do |score,index|
64
64
  @attributes_with_id[score] = "[#{index}|#{index + 1}]"
65
65
  end
66
- rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
66
+ rbm = InheritingFromRedisBackedModel.new(@attributes_with_id)
67
67
  rbm.send(:scores).each do |score|
68
68
  score.class.should eq(RedisBackedModel::SortedSet)
69
69
  end
@@ -72,28 +72,28 @@ describe RedisBackedModel do
72
72
  it "does not add near matches to scores instance variable, so it tries to add it as an instance variable instead, raising a name error because of []" do
73
73
  ['score_[foobar]', 'score[foo|bar]', 'score_[foobar|]'].each_with_index do |s,i|
74
74
  @attributes_with_id[s] = '[i|i+1]'
75
- expect { rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id) }.to raise_error(NameError)
75
+ expect { rbm = InheritingFromRedisBackedModel.new(@attributes_with_id) }.to raise_error(NameError)
76
76
  end
77
77
  end
78
78
 
79
79
  it "returns a redis command to add the model id to a set named (model_name)_ids" do
80
- rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
81
- rbm.send(:id_set_command).should eq('sadd|redis_backed_model_ids|1')
80
+ rbm = InheritingFromRedisBackedModel.new(@attributes_with_id)
81
+ rbm.send(:id_set_command).should eq("sadd|#{rbm.class.to_s.underscore}_ids|1")
82
82
  end
83
83
 
84
84
  context "creating an hset command for an instance variable" do
85
85
  before(:each) do
86
- @rbm = RedisBackedModel::RedisBackedModel.new()
86
+ @rbm = InheritingFromRedisBackedModel.new()
87
87
  @rbm.instance_variable_set('@id', 1)
88
88
  @rbm.instance_variable_set('@foo', 20)
89
89
  end
90
90
  it "creates a hset command for instance variables" do
91
- @rbm.send(:instance_variable_to_redis, '@foo').should eq('hset|redis_backed_model:1|foo|20')
91
+ @rbm.send(:instance_variable_to_redis, '@foo').should eq("hset|#{@rbm.class.to_s.underscore}:1|foo|20")
92
92
  end
93
93
 
94
94
  it "puts a underscored version of the model name as the first part of the hset key name in instance_variable_set" do
95
95
  hset_command = @rbm.send(:instance_variable_to_redis, '@foo')
96
- hset_command.split('|')[1].split(':')[0].should eq(RedisBackedModel.to_s.underscore)
96
+ hset_command.split('|')[1].split(':')[0].should eq(InheritingFromRedisBackedModel.to_s.underscore)
97
97
  end
98
98
 
99
99
  it "puts the id as the second part of the hset hset key name in instance_variable_set" do
@@ -120,13 +120,13 @@ describe RedisBackedModel do
120
120
 
121
121
  it "includes as sadd command in to_redis" do
122
122
  @attributes_with_id['score_[foo|bar]'] = '[1|2012-03-04]'
123
- rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
123
+ rbm = InheritingFromRedisBackedModel.new(@attributes_with_id)
124
124
  rbm.to_redis.select { |command| command.match(/sadd/)}.count.should eq(1)
125
125
  end
126
126
 
127
127
  it "includes a hset command for each instance variable except scores in to_redis" do
128
128
  @attributes_with_id['score_[foo|bar]'] = '[1|2012-03-04]'
129
- rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
129
+ rbm = InheritingFromRedisBackedModel.new(@attributes_with_id)
130
130
  expected = rbm.instance_variables.count - 1
131
131
  rbm.to_redis.select {|command| command.match(/hset/)}.count.should eq(expected)
132
132
  end
@@ -136,22 +136,59 @@ describe RedisBackedModel do
136
136
  scores.each_with_index do |score,index|
137
137
  @attributes_with_id[score] = "[#{index}|#{index + 1}]"
138
138
  end
139
- rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
139
+ rbm = InheritingFromRedisBackedModel.new(@attributes_with_id)
140
140
  rbm.to_redis.select { |command| command.match(/zadd/) }.count.should eq(scores.count)
141
141
  end
142
142
 
143
143
  it "should not error if there are no scores" do
144
- rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
144
+ rbm = InheritingFromRedisBackedModel.new(@attributes_with_id)
145
145
  rbm.to_redis.select { |command| command.include?('score')}.count.should eq(0)
146
146
  end
147
147
 
148
148
  it "should convert symbol attributes to strings" do
149
149
  attributes = {:id => 1, :first_name => 'jane', :last_name => 'doe'}
150
- rbm = RedisBackedModel::RedisBackedModel.new(attributes)
150
+ rbm = InheritingFromRedisBackedModel.new(attributes)
151
151
  rbm.instance_variables.include?(:@id).should eq(true)
152
152
  rbm.instance_variable_get(:@id).should eq(1)
153
153
  end
154
154
 
155
+ context "class method 'find'" do
156
+ before(:each) do
157
+ $redis.hset('inheriting_from_redis_backed_model:0', 'foo', 'bar')
158
+ $redis.hset('inheriting_from_redis_backed_model:1', 'wibble', 'wobble')
159
+ end
160
+ it "should return an array if objects found for ids" do
161
+ found = InheritingFromRedisBackedModel.find([0, 1])
162
+ found.should be_instance_of(Array)
163
+ found.count.should eq(2)
164
+ end
165
+ it "should have objects in the array" do
166
+ found = InheritingFromRedisBackedModel.find([0, 1])
167
+ found.each do |f|
168
+ f.should be_instance_of(InheritingFromRedisBackedModel)
169
+ end
170
+ end
171
+ it "should return an object if only one item is found" do
172
+ found = InheritingFromRedisBackedModel.find(0)
173
+ found.should be_instance_of(InheritingFromRedisBackedModel)
174
+ found.send(:id).should eq(0)
175
+ end
176
+ it "should return an object if only one item is found regardless of arguments" do
177
+ found = InheritingFromRedisBackedModel.find(0,2,3,4,5,6,7)
178
+ found.should be_instance_of(InheritingFromRedisBackedModel)
179
+ found.send(:id).should eq(0)
180
+ end
181
+ it "should return empty array if nothing found" do
182
+ found = InheritingFromRedisBackedModel.find(2,3,4,5,6,7)
183
+ found.should be_instance_of(Array)
184
+ found.count.should eq(0)
185
+ end
186
+ after(:each) do
187
+ $redis.hdel('inheriting_from_redis_backed_model:0', 'foo')
188
+ $redis.hdel('inheriting_from_redis_backed_model:1', 'wibble')
189
+ end
190
+ end
191
+
155
192
  end
156
193
 
157
194
  # test inheriting from RedisBackedModel
@@ -174,4 +211,8 @@ describe Person do
174
211
  person.name.should eq('jane doe')
175
212
  end
176
213
 
214
+ after(:all) do
215
+ $redis.hdel 'person:0', 'first_name'
216
+ $redis.hdel 'person:0', 'last_name'
217
+ end
177
218
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_backed_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-09-04 00:00:00.000000000Z
13
+ date: 2012-09-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &2164484940 !ruby/object:Gem::Requirement
17
+ requirement: !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,15 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2164484940
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
26
31
  - !ruby/object:Gem::Dependency
27
32
  name: redis
28
- requirement: &2164484520 !ruby/object:Gem::Requirement
33
+ requirement: !ruby/object:Gem::Requirement
29
34
  none: false
30
35
  requirements:
31
36
  - - ! '>='
@@ -33,10 +38,15 @@ dependencies:
33
38
  version: '0'
34
39
  type: :development
35
40
  prerelease: false
36
- version_requirements: *2164484520
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
37
47
  - !ruby/object:Gem::Dependency
38
48
  name: activesupport
39
- requirement: &2164484100 !ruby/object:Gem::Requirement
49
+ requirement: !ruby/object:Gem::Requirement
40
50
  none: false
41
51
  requirements:
42
52
  - - ! '>='
@@ -44,7 +54,12 @@ dependencies:
44
54
  version: '0'
45
55
  type: :runtime
46
56
  prerelease: false
47
- version_requirements: *2164484100
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
48
63
  description: Provides methods to models that are backed by a redis instance.
49
64
  email:
50
65
  - iwhitney@ssa-i.org
@@ -89,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
104
  version: '0'
90
105
  requirements: []
91
106
  rubyforge_project:
92
- rubygems_version: 1.8.16
107
+ rubygems_version: 1.8.21
93
108
  signing_key:
94
109
  specification_version: 3
95
110
  summary: Provides methods for the creation of redis-backed models, specifically the