redis_backed_model 0.0.2 → 0.0.3
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.
@@ -13,12 +13,9 @@ module RedisBackedModel
|
|
13
13
|
|
14
14
|
def to_redis
|
15
15
|
redis_commands = []
|
16
|
-
redis_commands <<
|
16
|
+
redis_commands << id_set_command
|
17
17
|
instance_variables.each do | var |
|
18
|
-
|
19
|
-
end
|
20
|
-
scores.each do |score|
|
21
|
-
redis_commands << score.to_redis
|
18
|
+
build_command_for_variable(var, redis_commands)
|
22
19
|
end
|
23
20
|
redis_commands
|
24
21
|
end
|
@@ -40,18 +37,29 @@ module RedisBackedModel
|
|
40
37
|
@scores << SortedSet.new(self.class, id, Hash[key,value])
|
41
38
|
end
|
42
39
|
|
40
|
+
def build_command_for_variable(variable, collection)
|
41
|
+
value = instance_variable_get(variable)
|
42
|
+
if value.respond_to?(:each)
|
43
|
+
value.each do |redis_object|
|
44
|
+
collection << redis_object.to_redis
|
45
|
+
end
|
46
|
+
else
|
47
|
+
collection << instance_variable_to_redis(variable)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def id_set_command
|
52
|
+
"sadd|#{model_name_for_redis}_ids|#{id}"
|
53
|
+
end
|
54
|
+
|
43
55
|
def instance_variable_to_redis(instance_variable)
|
44
|
-
"hset
|
56
|
+
"hset|#{model_name_for_redis}:#{id}|#{instance_variable.to_s.deinstance_variableize}|#{instance_variable_get(instance_variable.to_s)}"
|
45
57
|
end
|
46
58
|
|
47
59
|
def model_name_for_redis
|
48
60
|
class_as_string = self.class.to_s.demodulize.underscore
|
49
61
|
end
|
50
|
-
|
51
|
-
def redis_set_command
|
52
|
-
"sadd #{model_name_for_redis}_ids #{id}"
|
53
|
-
end
|
54
|
-
|
62
|
+
|
55
63
|
end
|
56
64
|
|
57
65
|
end
|
@@ -44,8 +44,8 @@ describe RedisBackedModel::SortedSet do
|
|
44
44
|
@scored_set.send(:member).should eq(@scored_set.send(:model_id))
|
45
45
|
end
|
46
46
|
|
47
|
-
it "returns 'zadd
|
48
|
-
@scored_set.to_redis.should eq('zadd
|
47
|
+
it "returns 'zadd|key|value|model_id' as to_redis" do
|
48
|
+
@scored_set.to_redis.should eq('zadd|false_classes_for_foo_by_bar:wibble|wobble|1')
|
49
49
|
end
|
50
50
|
|
51
51
|
end
|
@@ -7,14 +7,14 @@ describe RedisBackedModel do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
before(:each) do
|
10
|
-
@
|
10
|
+
@attributes_with_id = {}
|
11
11
|
@size = rand(9) + 1
|
12
12
|
key_seed = 'abcdefghijklmn'
|
13
13
|
(1..@size).each do | i |
|
14
|
-
key = key_seed[i..(rand(i)+i)]
|
15
|
-
@
|
14
|
+
key = key_seed[i..(rand(i)+i)]
|
15
|
+
@attributes_with_id[key] = i
|
16
16
|
end
|
17
|
-
@
|
17
|
+
@attributes_with_id['id'] = 1
|
18
18
|
@size += 1
|
19
19
|
end
|
20
20
|
|
@@ -30,9 +30,9 @@ describe RedisBackedModel do
|
|
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(@
|
33
|
+
rbm = RedisBackedModel::RedisBackedModel.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}"
|
37
37
|
rbm.instance_variables.include?(key.instance_variableize).should eq(true), "no instance variable for #{key}"
|
38
38
|
end
|
@@ -43,25 +43,27 @@ describe RedisBackedModel do
|
|
43
43
|
expect { RedisBackedModel::RedisBackedModel.new(['w', 1]) }.to raise_error(ArgumentError)
|
44
44
|
end
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
46
|
+
context "initializing with the attribute hash contains a score_[|] key" do
|
47
|
+
before(:each) do
|
48
|
+
@score_key = 'score_[foo|bar]'
|
49
|
+
@attributes_with_id[@score_key] = '[1|2]'
|
50
|
+
end
|
51
|
+
it "does not create a key for any score_[|] attribute" do
|
52
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
|
53
|
+
rbm.instance_variables.include?(@score_key.instance_variableize).should eq(false)
|
54
|
+
end
|
52
55
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
rbm.instance_variables.include?('scores'.instance_variableize).should eq(true)
|
56
|
+
it "creates a scores instance variable if there are any score_[x|y] attributes" do
|
57
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
|
58
|
+
rbm.instance_variables.include?('scores'.instance_variableize).should eq(true)
|
59
|
+
end
|
58
60
|
end
|
59
61
|
|
60
|
-
it "stores score_ attributes
|
62
|
+
it "stores score_ attributes as SortedSet objects in scores" do
|
61
63
|
['score_[foo|bar]', 'score_[baz|qux]', 'score_[wibble|wobble]'].each_with_index do |score,index|
|
62
|
-
@
|
64
|
+
@attributes_with_id[score] = "[#{index}|#{index + 1}]"
|
63
65
|
end
|
64
|
-
rbm = RedisBackedModel::RedisBackedModel.new(@
|
66
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
|
65
67
|
rbm.send(:scores).each do |score|
|
66
68
|
score.class.should eq(RedisBackedModel::SortedSet)
|
67
69
|
end
|
@@ -69,70 +71,62 @@ describe RedisBackedModel do
|
|
69
71
|
|
70
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
|
71
73
|
['score_[foobar]', 'score[foo|bar]', 'score_[foobar|]'].each_with_index do |s,i|
|
72
|
-
@
|
73
|
-
expect { rbm = RedisBackedModel::RedisBackedModel.new(@
|
74
|
+
@attributes_with_id[s] = '[i|i+1]'
|
75
|
+
expect { rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id) }.to raise_error(NameError)
|
74
76
|
end
|
75
77
|
end
|
76
78
|
|
77
|
-
it "returns a redis command to
|
78
|
-
rbm = RedisBackedModel::RedisBackedModel.new(@
|
79
|
-
rbm.send(:
|
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
82
|
end
|
81
83
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
84
|
+
context "creating an hset command for an instance variable" do
|
85
|
+
before(:each) do
|
86
|
+
@rbm = RedisBackedModel::RedisBackedModel.new()
|
87
|
+
@rbm.instance_variable_set('@id', 1)
|
88
|
+
@rbm.instance_variable_set('@foo', 20)
|
89
|
+
end
|
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')
|
92
|
+
end
|
88
93
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
hset_command = rbm.send(:instance_variable_to_redis, '@foo')
|
94
|
-
hset_command.split(' ')[1].split(':')[0].should eq(RedisBackedModel.to_s.underscore)
|
95
|
-
end
|
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
|
+
hset_command = @rbm.send(:instance_variable_to_redis, '@foo')
|
96
|
+
hset_command.split('|')[1].split(':')[0].should eq(RedisBackedModel.to_s.underscore)
|
97
|
+
end
|
96
98
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
hset_command = rbm.send(:instance_variable_to_redis, '@foo')
|
102
|
-
hset_command.split(' ')[1].split(':')[1].should eq(rbm.instance_variable_get('@id').to_s)
|
103
|
-
end
|
99
|
+
it "puts the id as the second part of the hset hset key name in instance_variable_set" do
|
100
|
+
hset_command = @rbm.send(:instance_variable_to_redis, '@foo')
|
101
|
+
hset_command.split('|')[1].split(':')[1].should eq(@rbm.instance_variable_get('@id').to_s)
|
102
|
+
end
|
104
103
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
hset_command = rbm.send(:instance_variable_to_redis, '@foo')
|
110
|
-
hset_command.split(' ')[2].should eq('foo')
|
111
|
-
end
|
104
|
+
it "puts the instance_variable_name (without @) as the hset field name in instance_variable_set" do
|
105
|
+
hset_command = @rbm.send(:instance_variable_to_redis, '@foo')
|
106
|
+
hset_command.split('|')[2].should eq('foo')
|
107
|
+
end
|
112
108
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
109
|
+
it "put the instance variable value as the hset value in instance_variable_set" do
|
110
|
+
hset_command = @rbm.send(:instance_variable_to_redis, '@foo')
|
111
|
+
hset_command.split('|')[3].should eq('20')
|
112
|
+
end
|
113
|
+
|
114
|
+
it "only ever has 4 arguments" do
|
115
|
+
@rbm.instance_variable_set('@bar', 'string with spaces')
|
116
|
+
hset_command = @rbm.send(:instance_variable_to_redis, '@bar')
|
117
|
+
hset_command.split('|').count.should eq(4)
|
118
|
+
end
|
119
119
|
end
|
120
120
|
|
121
|
-
it "does not create an hset command for the id instance variable" do
|
122
|
-
rbm = RedisBackedModel::RedisBackedModel.new()
|
123
|
-
rbm.instance_variable_set('@id', 1)
|
124
|
-
rbm.send(:instance_variable_to_redis, '@id').should eq(nil)
|
125
|
-
end
|
126
|
-
|
127
121
|
it "includes as sadd command in to_redis" do
|
128
|
-
@
|
129
|
-
rbm = RedisBackedModel::RedisBackedModel.new(@
|
122
|
+
@attributes_with_id['score_[foo|bar]'] = '[1|2012-03-04]'
|
123
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
|
130
124
|
rbm.to_redis.select { |command| command.match(/sadd/)}.count.should eq(1)
|
131
125
|
end
|
132
126
|
|
133
|
-
it "includes a hset command for each instance variable except
|
134
|
-
@
|
135
|
-
rbm = RedisBackedModel::RedisBackedModel.new(@
|
127
|
+
it "includes a hset command for each instance variable except scores in to_redis" do
|
128
|
+
@attributes_with_id['score_[foo|bar]'] = '[1|2012-03-04]'
|
129
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
|
136
130
|
expected = rbm.instance_variables.count - 1
|
137
131
|
rbm.to_redis.select {|command| command.match(/hset/)}.count.should eq(expected)
|
138
132
|
end
|
@@ -140,11 +134,23 @@ describe RedisBackedModel do
|
|
140
134
|
it "includes a sorted_set.to_redis command for each score attribute in to_redis" do
|
141
135
|
scores = ['score_[foo|bar]', 'score_[baz|qux]', 'score_[wibble|wobble]']
|
142
136
|
scores.each_with_index do |score,index|
|
143
|
-
@
|
137
|
+
@attributes_with_id[score] = "[#{index}|#{index + 1}]"
|
144
138
|
end
|
145
|
-
rbm = RedisBackedModel::RedisBackedModel.new(@
|
139
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
|
146
140
|
rbm.to_redis.select { |command| command.match(/zadd/) }.count.should eq(scores.count)
|
147
141
|
end
|
142
|
+
|
143
|
+
it "should not error if there are no scores" do
|
144
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes_with_id)
|
145
|
+
rbm.to_redis.select { |command| command.include?('score')}.count.should eq(0)
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should convert symbol attributes to strings" do
|
149
|
+
attributes = {:id => 1, :first_name => 'jane', :last_name => 'doe'}
|
150
|
+
rbm = RedisBackedModel::RedisBackedModel.new(attributes)
|
151
|
+
rbm.instance_variables.include?(:@id).should eq(true)
|
152
|
+
rbm.instance_variable_get(:@id).should eq(1)
|
153
|
+
end
|
148
154
|
|
149
155
|
end
|
150
156
|
|
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
|
+
version: 0.0.3
|
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-08-
|
13
|
+
date: 2012-08-31 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
17
|
-
requirement: &
|
17
|
+
requirement: &2156372240 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2156372240
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: redis
|
28
|
-
requirement: &
|
28
|
+
requirement: &2156371820 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *2156371820
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: activesupport
|
39
|
-
requirement: &
|
39
|
+
requirement: &2156371400 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,7 +44,7 @@ dependencies:
|
|
44
44
|
version: '0'
|
45
45
|
type: :runtime
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *2156371400
|
48
48
|
description: Provides methods to models that are backed by a redis instance.
|
49
49
|
email:
|
50
50
|
- iwhitney@ssa-i.org
|