redis-orm 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +13 -0
- data/lib/redis/actions/destroying.rb +1 -1
- data/lib/redis/actions/finding.rb +5 -5
- data/lib/redis/actions/saving.rb +4 -4
- data/lib/redis/orm.rb +5 -5
- data/lib/redis/orm/version.rb +1 -1
- data/lib/redis/relations/belongs_to.rb +4 -4
- data/lib/redis/relations/has_many.rb +6 -6
- data/lib/redis/relations/has_one.rb +4 -4
- data/lib/redis/validations/uniqueness.rb +6 -6
- data/spec/redis/actions/finding_spec.rb +2 -2
- data/spec/redis/orm_spec.rb +4 -4
- data/spec/redis/relations/belongs_to_spec.rb +1 -27
- data/spec/redis/relations/has_many_spec.rb +2 -2
- data/spec/redis/relations/has_one_spec.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -178,6 +178,9 @@ You can use these callbacks to perform commands directly upon the Redis database
|
|
178
178
|
|
179
179
|
## Configuration
|
180
180
|
|
181
|
+
|
182
|
+
### connection
|
183
|
+
|
181
184
|
You can set the host and port for Redis:
|
182
185
|
|
183
186
|
Redis.host = 'localhost'
|
@@ -187,3 +190,13 @@ If you already have an active connection, however, these changes will not take e
|
|
187
190
|
|
188
191
|
Redis.connection = Redis.new(:host => 'localhost', :port => 3000)
|
189
192
|
|
193
|
+
|
194
|
+
### serializer
|
195
|
+
|
196
|
+
By default, Redis serializes the attributes using Marshal. If you would prefer to store data in some other format, simply replace Redis::ORM.serializer with your choice. Anything that responds to #dump and #load will work (JSON, YAML, etc):
|
197
|
+
|
198
|
+
Redis::ORM.serializer = YAML
|
199
|
+
|
200
|
+
Obviously, you can use your own custom serializer just as easily. Just pass in an object that returns a string for `#dump(object)` and an object for `#load(string)` and you're good to go.
|
201
|
+
|
202
|
+
It's worth noting that there can only be one serializer for all models. It's also worth noting that it's a Bad Idea to switch serializers once you've got a database full of data. If you need to switch, for instance, from JSON to YAML serialization, you'll need to come up with a migration scheme yourself.
|
@@ -13,7 +13,7 @@ module Redis::Actions::Destroying
|
|
13
13
|
# run_callbacks(:before_destroy)
|
14
14
|
run_callbacks(:destroy) do
|
15
15
|
transaction do
|
16
|
-
connection.del
|
16
|
+
connection.del id
|
17
17
|
within_destroy_blocks.each do |method_name_or_block|
|
18
18
|
if method_name_or_block.kind_of?(String) || method_name_or_block.kind_of?(Symbol)
|
19
19
|
send method_name_or_block
|
@@ -4,10 +4,10 @@ module Redis::Actions::Finding
|
|
4
4
|
end
|
5
5
|
|
6
6
|
module ClassMethods
|
7
|
-
def find(
|
8
|
-
data = connection.get(
|
7
|
+
def find(id)
|
8
|
+
data = connection.get(id)
|
9
9
|
if data
|
10
|
-
klass_name =
|
10
|
+
klass_name = id.split(/\//)[0]
|
11
11
|
klass = (klass_name.camelize.constantize rescue self)
|
12
12
|
instance = klass.new(serializer.load(data))
|
13
13
|
instance.set_unchanged!
|
@@ -18,8 +18,8 @@ module Redis::Actions::Finding
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def all
|
21
|
-
connection.hgetall(File.join(model_name, "
|
22
|
-
find(
|
21
|
+
connection.hgetall(File.join(model_name, "ids")).collect do |id|
|
22
|
+
find(id.first)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
data/lib/redis/actions/saving.rb
CHANGED
@@ -10,7 +10,7 @@ module Redis::Actions::Saving
|
|
10
10
|
|
11
11
|
def save
|
12
12
|
if valid?
|
13
|
-
|
13
|
+
define_id if id.nil?
|
14
14
|
run_callbacks(:save) do
|
15
15
|
transaction do
|
16
16
|
within_save_blocks.each do |block_or_method|
|
@@ -20,7 +20,7 @@ module Redis::Actions::Saving
|
|
20
20
|
block_or_method.call self
|
21
21
|
end
|
22
22
|
end
|
23
|
-
connection.set(
|
23
|
+
connection.set(id, serializer.dump(attributes))
|
24
24
|
end
|
25
25
|
end
|
26
26
|
set_unchanged!
|
@@ -34,8 +34,8 @@ module Redis::Actions::Saving
|
|
34
34
|
raise "Record was not saved: #{errors.full_messages}" unless save
|
35
35
|
end
|
36
36
|
|
37
|
-
def
|
38
|
-
self.
|
37
|
+
def define_id
|
38
|
+
self.id = File.join(model_name, connection.incr("__uniq__").to_s)
|
39
39
|
end
|
40
40
|
|
41
41
|
module ClassMethods
|
data/lib/redis/orm.rb
CHANGED
@@ -23,19 +23,19 @@ class Redis::ORM
|
|
23
23
|
include Redis::Relations
|
24
24
|
include Redis::Validations
|
25
25
|
|
26
|
-
attribute :
|
27
|
-
validates_uniqueness_of :
|
26
|
+
attribute :id
|
27
|
+
validates_uniqueness_of :id
|
28
28
|
|
29
29
|
class << self
|
30
30
|
delegate :connection, :to => :Redis
|
31
31
|
end
|
32
32
|
|
33
33
|
def to_key
|
34
|
-
persisted? ?
|
34
|
+
persisted? ? id : nil
|
35
35
|
end
|
36
36
|
|
37
37
|
def to_param
|
38
|
-
persisted? ? File.join(model_name,
|
38
|
+
persisted? ? File.join(model_name, id) : nil
|
39
39
|
end
|
40
40
|
|
41
41
|
def persisted?
|
@@ -54,6 +54,6 @@ class Redis::ORM
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def ==(other)
|
57
|
-
other &&
|
57
|
+
other && id == other.id
|
58
58
|
end
|
59
59
|
end
|
data/lib/redis/orm/version.rb
CHANGED
@@ -11,21 +11,21 @@ module Redis::Relations::BelongsTo
|
|
11
11
|
if belongs_to_references.key?(name)
|
12
12
|
belongs_to_references[name]
|
13
13
|
else
|
14
|
-
result = self.class.find(connection.hget(
|
14
|
+
result = self.class.find(connection.hget(belongs_to_relation_id(name), id))
|
15
15
|
belongs_to_references[name] = result
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
19
|
+
def belongs_to_relation_id(name)
|
20
20
|
File.join("references", belongs_to_relations[name][:relation].to_s)
|
21
21
|
end
|
22
22
|
|
23
23
|
def save_belongs_to_references
|
24
24
|
belongs_to_references.each do |relation_name, reference|
|
25
25
|
if reference
|
26
|
-
reference = reference.
|
26
|
+
reference = reference.id
|
27
27
|
end
|
28
|
-
connection.hset(
|
28
|
+
connection.hset(belongs_to_relation_id(relation_name), id, reference)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -7,7 +7,7 @@ module Redis::Relations::HasMany
|
|
7
7
|
if has_many_references.key?(relation_name)
|
8
8
|
has_many_references[relation_name]
|
9
9
|
else
|
10
|
-
result = connection.hget(
|
10
|
+
result = connection.hget(has_many_relation_id(relation_name), id)
|
11
11
|
if result
|
12
12
|
result = serializer.load(result)
|
13
13
|
if result.respond_to?(:collect)
|
@@ -24,15 +24,15 @@ module Redis::Relations::HasMany
|
|
24
24
|
|
25
25
|
def save_has_many_references
|
26
26
|
has_many_references.each do |relation_name, array|
|
27
|
-
array = array.collect { |a| a.
|
28
|
-
connection.hset(
|
29
|
-
array.each do |
|
30
|
-
connection.hset(
|
27
|
+
array = array.collect { |a| a.id }
|
28
|
+
connection.hset(has_many_relation_id(relation_name), id, serializer.dump(array))
|
29
|
+
array.each do |aid|
|
30
|
+
connection.hset(has_many_relation_id(relation_name), aid, id)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
def
|
35
|
+
def has_many_relation_id(name)
|
36
36
|
File.join("references", has_many_relations[name][:relation].to_s)
|
37
37
|
end
|
38
38
|
|
@@ -11,20 +11,20 @@ module Redis::Relations::HasOne
|
|
11
11
|
if has_one_references.key?(relation_name)
|
12
12
|
has_one_references[relation_name]
|
13
13
|
else
|
14
|
-
result = self.class.find(connection.hget(
|
14
|
+
result = self.class.find(connection.hget(has_one_relation_id(relation_name), id))
|
15
15
|
has_one_references[relation_name] = result
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
19
|
+
def has_one_relation_id(name)
|
20
20
|
File.join("references", has_one_relations[name][:relation].to_s)
|
21
21
|
end
|
22
22
|
|
23
23
|
def save_has_one_references
|
24
24
|
has_one_references.each do |relation_name, reference|
|
25
25
|
if reference
|
26
|
-
connection.hset(
|
27
|
-
connection.hset(
|
26
|
+
connection.hset(has_one_relation_id(relation_name), id, reference.id)
|
27
|
+
connection.hset(has_one_relation_id(relation_name), reference.id, id)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module Redis::Validations::Uniqueness
|
2
|
-
def
|
2
|
+
def uniqueness_id(attribute_name)
|
3
3
|
File.join(model_name, attribute_name.to_s.pluralize)
|
4
4
|
end
|
5
5
|
|
@@ -16,8 +16,8 @@ module Redis::Validations::Uniqueness
|
|
16
16
|
|
17
17
|
validate do |record|
|
18
18
|
record.unique_fields.each do |name|
|
19
|
-
if
|
20
|
-
if
|
19
|
+
if id_in_use = connection.hget(record.uniqueness_id(name), record.send(name))
|
20
|
+
if id_in_use != record.id
|
21
21
|
record.errors.add(name, "must be unique")
|
22
22
|
end
|
23
23
|
end
|
@@ -26,14 +26,14 @@ module Redis::Validations::Uniqueness
|
|
26
26
|
|
27
27
|
within_save_block do |record|
|
28
28
|
record.unique_fields.each do |name|
|
29
|
-
connection.hdel(record.
|
30
|
-
connection.hset(record.
|
29
|
+
connection.hdel(record.uniqueness_id(name), record.previous_attributes[name])
|
30
|
+
connection.hset(record.uniqueness_id(name), record.send(name), record.id)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
within_destroy_block do |record|
|
35
35
|
record.unique_fields.each do |name|
|
36
|
-
connection.hdel(record.
|
36
|
+
connection.hdel(record.uniqueness_id(name), record.send(name))
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
@@ -17,9 +17,9 @@ describe Redis::Actions::Finding do
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
context "with
|
20
|
+
context "with id not matching known constants" do
|
21
21
|
it "should find record with its own class" do
|
22
|
-
orm_class.create!(:
|
22
|
+
orm_class.create!(:id => '1234')
|
23
23
|
orm_class.find('1234').should be_kind_of(orm_class)
|
24
24
|
end
|
25
25
|
end
|
data/spec/redis/orm_spec.rb
CHANGED
@@ -39,16 +39,16 @@ describe Redis::ORM do
|
|
39
39
|
subject.should_not be_changed
|
40
40
|
end
|
41
41
|
|
42
|
-
it "should have a
|
43
|
-
subject.
|
42
|
+
it "should have a id" do
|
43
|
+
subject.id.should_not be_nil
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should be find-able" do
|
47
|
-
orm_class.find(subject.
|
47
|
+
orm_class.find(subject.id).should == subject
|
48
48
|
end
|
49
49
|
|
50
50
|
it "should find separate instances" do
|
51
|
-
orm_class.find(subject.
|
51
|
+
orm_class.find(subject.id).should_not be(subject)
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
@@ -12,7 +12,7 @@ describe Redis::Relations::BelongsTo do
|
|
12
12
|
subject.other = other
|
13
13
|
subject.save!
|
14
14
|
|
15
|
-
orm_class.find(subject.
|
15
|
+
orm_class.find(subject.id).other.should be_kind_of(orm_class)
|
16
16
|
end
|
17
17
|
|
18
18
|
it "mass assignment" do
|
@@ -26,29 +26,3 @@ describe Redis::Relations::BelongsTo do
|
|
26
26
|
subject.save!
|
27
27
|
end
|
28
28
|
end
|
29
|
-
|
30
|
-
|
31
|
-
=begin
|
32
|
-
|
33
|
-
class Aye
|
34
|
-
has_many bees(, :foreign_key => :aye_id)
|
35
|
-
end
|
36
|
-
|
37
|
-
class Bee
|
38
|
-
(attribute :aye_id)
|
39
|
-
belongs_to aye
|
40
|
-
end
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
# reference built on the relation (foreign_key):
|
45
|
-
# has_many uses its own name while belongs_to uses
|
46
|
-
# the reference name
|
47
|
-
|
48
|
-
references/ayes
|
49
|
-
Aye/1 => [ Bee/1, Bee/2 ] # has_many stores an array of keys
|
50
|
-
Bee/1 => Aye/1 # belongs_to stores a single key
|
51
|
-
Bee/2 => Aye/1
|
52
|
-
|
53
|
-
|
54
|
-
=end
|
@@ -14,9 +14,9 @@ describe Redis::Relations::HasMany do
|
|
14
14
|
end
|
15
15
|
subject.save!
|
16
16
|
|
17
|
-
orm_class.find(subject.
|
17
|
+
orm_class.find(subject.id).others.should have(10).items
|
18
18
|
for i in 0...10
|
19
|
-
orm_class.find(subject.
|
19
|
+
orm_class.find(subject.id).others[i].should be_kind_of(orm_class)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|