remodel 0.1.4 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +19 -10
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/example/book.rb +1 -0
- data/lib/remodel/entity.rb +57 -45
- data/lib/remodel/has_many.rb +12 -8
- data/lib/remodel.rb +2 -1
- data/test/helper.rb +1 -1
- data/test/test_entity.rb +47 -96
- data/test/test_entity_defaults.rb +55 -0
- data/test/test_entity_delete.rb +61 -0
- data/test/test_many_to_many.rb +7 -7
- data/test/test_many_to_one.rb +28 -15
- data/test/test_mappers.rb +11 -11
- data/test/test_one_to_many.rb +22 -22
- data/test/test_one_to_one.rb +4 -4
- metadata +22 -12
- data/.gitignore +0 -2
- data/lib/remodel/has_one.rb +0 -57
- data/remodel.gemspec +0 -72
data/test/test_one_to_many.rb
CHANGED
@@ -16,65 +16,65 @@ class TestOneToMany < Test::Unit::TestCase
|
|
16
16
|
context "has_one" do
|
17
17
|
context "association getter" do
|
18
18
|
should "exist" do
|
19
|
-
assert Piece.create.respond_to?(:puzzle)
|
19
|
+
assert Piece.create('cx').respond_to?(:puzzle)
|
20
20
|
end
|
21
21
|
|
22
22
|
should "return nil by default" do
|
23
|
-
assert_nil Piece.create.puzzle
|
23
|
+
assert_nil Piece.create('cx').puzzle
|
24
24
|
end
|
25
25
|
|
26
26
|
should "return the associated entity" do
|
27
|
-
puzzle = Puzzle.create :topic => 'animals'
|
28
|
-
piece = Piece.create
|
29
|
-
redis.
|
27
|
+
puzzle = Puzzle.create('cx', :topic => 'animals')
|
28
|
+
piece = Piece.create('cx')
|
29
|
+
redis.hset('cx', "#{piece.key}_puzzle", puzzle.key)
|
30
30
|
assert_equal 'animals', piece.puzzle.topic
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
context "association setter" do
|
35
35
|
should "exist" do
|
36
|
-
assert Piece.create.respond_to?(:'puzzle=')
|
36
|
+
assert Piece.create('cx').respond_to?(:'puzzle=')
|
37
37
|
end
|
38
38
|
|
39
39
|
should "store the key of the associated entity" do
|
40
|
-
puzzle = Puzzle.create
|
41
|
-
piece = Piece.create
|
40
|
+
puzzle = Puzzle.create('cx')
|
41
|
+
piece = Piece.create('cx')
|
42
42
|
piece.puzzle = puzzle
|
43
|
-
assert_equal puzzle.key, redis.
|
43
|
+
assert_equal puzzle.key, redis.hget(puzzle.context, "#{piece.key}_puzzle")
|
44
44
|
end
|
45
45
|
|
46
46
|
should "add the entity to the reverse association" do
|
47
|
-
puzzle = Puzzle.create
|
48
|
-
piece = Piece.create
|
47
|
+
puzzle = Puzzle.create('cx')
|
48
|
+
piece = Piece.create('cx')
|
49
49
|
piece.puzzle = puzzle
|
50
50
|
assert_equal 1, puzzle.pieces.size
|
51
51
|
assert_equal piece.id, puzzle.pieces.first.id
|
52
52
|
end
|
53
53
|
|
54
54
|
should "remove the entity from the old reverse association" do
|
55
|
-
puzzle = Puzzle.create
|
55
|
+
puzzle = Puzzle.create('cx')
|
56
56
|
piece = puzzle.pieces.create
|
57
|
-
new_puzzle = Puzzle.create
|
57
|
+
new_puzzle = Puzzle.create('cx')
|
58
58
|
piece.puzzle = new_puzzle
|
59
59
|
assert_equal [], puzzle.reload.pieces
|
60
60
|
end
|
61
61
|
|
62
62
|
should "be settable to nil" do
|
63
|
-
piece = Piece.create
|
63
|
+
piece = Piece.create('cx')
|
64
64
|
piece.puzzle = nil
|
65
65
|
assert_nil piece.puzzle
|
66
66
|
end
|
67
67
|
|
68
68
|
should "remove the key if set to nil" do
|
69
|
-
piece = Piece.create
|
70
|
-
piece.puzzle = Puzzle.create
|
69
|
+
piece = Piece.create('cx')
|
70
|
+
piece.puzzle = Puzzle.create('cx')
|
71
71
|
piece.puzzle = nil
|
72
|
-
assert_nil redis.
|
72
|
+
assert_nil redis.hget(piece.context, "#{piece.key}_puzzle")
|
73
73
|
end
|
74
74
|
|
75
75
|
should "remove the entity from the reverse association if set to nil" do
|
76
|
-
puzzle = Puzzle.create
|
77
|
-
piece = Piece.create
|
76
|
+
puzzle = Puzzle.create('cx')
|
77
|
+
piece = Piece.create('cx')
|
78
78
|
piece.puzzle = puzzle
|
79
79
|
piece.puzzle = nil
|
80
80
|
puzzle.reload
|
@@ -85,9 +85,9 @@ class TestOneToMany < Test::Unit::TestCase
|
|
85
85
|
|
86
86
|
context "reload" do
|
87
87
|
should "reset has_one associations" do
|
88
|
-
piece = Piece.create :color => 'black'
|
89
|
-
piece.puzzle = Puzzle.create
|
90
|
-
redis.
|
88
|
+
piece = Piece.create('cx', :color => 'black')
|
89
|
+
piece.puzzle = Puzzle.create('cx')
|
90
|
+
redis.hdel 'cx', "#{piece.key}_puzzle"
|
91
91
|
piece.reload
|
92
92
|
assert_nil piece.puzzle
|
93
93
|
end
|
data/test/test_one_to_one.rb
CHANGED
@@ -14,14 +14,14 @@ class TestOneToOne < Test::Unit::TestCase
|
|
14
14
|
|
15
15
|
context "both associations" do
|
16
16
|
should "be nil by default" do
|
17
|
-
assert_equal nil, Man.new.wife
|
18
|
-
assert_equal nil, Woman.new.husband
|
17
|
+
assert_equal nil, Man.new('cx').wife
|
18
|
+
assert_equal nil, Woman.new('cx').husband
|
19
19
|
end
|
20
20
|
|
21
21
|
context "setter" do
|
22
22
|
setup do
|
23
|
-
@bill = Man.create :name => 'Bill'
|
24
|
-
@mary = Woman.create :name => 'Mary'
|
23
|
+
@bill = Man.create('cx', :name => 'Bill')
|
24
|
+
@mary = Woman.create('cx', :name => 'Mary')
|
25
25
|
end
|
26
26
|
|
27
27
|
context "non-nil value" do
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: remodel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 3
|
8
|
+
- 0
|
9
|
+
version: 0.3.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Tim Lossen
|
@@ -9,11 +14,11 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2011-05-31 00:00:00 +02:00
|
13
18
|
default_executable: redis-monitor.rb
|
14
19
|
dependencies: []
|
15
20
|
|
16
|
-
description:
|
21
|
+
description: persist your objects to redis hashes.
|
17
22
|
email: tim@lossen.de
|
18
23
|
executables:
|
19
24
|
- redis-monitor.rb
|
@@ -23,7 +28,6 @@ extra_rdoc_files:
|
|
23
28
|
- LICENSE
|
24
29
|
- README.md
|
25
30
|
files:
|
26
|
-
- .gitignore
|
27
31
|
- LICENSE
|
28
32
|
- README.md
|
29
33
|
- Rakefile
|
@@ -35,11 +39,11 @@ files:
|
|
35
39
|
- lib/remodel.rb
|
36
40
|
- lib/remodel/entity.rb
|
37
41
|
- lib/remodel/has_many.rb
|
38
|
-
- lib/remodel/has_one.rb
|
39
42
|
- lib/remodel/mapper.rb
|
40
|
-
- remodel.gemspec
|
41
43
|
- test/helper.rb
|
42
44
|
- test/test_entity.rb
|
45
|
+
- test/test_entity_defaults.rb
|
46
|
+
- test/test_entity_delete.rb
|
43
47
|
- test/test_many_to_many.rb
|
44
48
|
- test/test_many_to_one.rb
|
45
49
|
- test/test_mappers.rb
|
@@ -51,32 +55,38 @@ homepage: http://github.com/tlossen/remodel
|
|
51
55
|
licenses: []
|
52
56
|
|
53
57
|
post_install_message:
|
54
|
-
rdoc_options:
|
55
|
-
|
58
|
+
rdoc_options: []
|
59
|
+
|
56
60
|
require_paths:
|
57
61
|
- lib
|
58
62
|
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
59
64
|
requirements:
|
60
65
|
- - ">="
|
61
66
|
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
62
69
|
version: "0"
|
63
|
-
version:
|
64
70
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
65
72
|
requirements:
|
66
73
|
- - ">="
|
67
74
|
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
68
77
|
version: "0"
|
69
|
-
version:
|
70
78
|
requirements: []
|
71
79
|
|
72
80
|
rubyforge_project:
|
73
|
-
rubygems_version: 1.3.
|
81
|
+
rubygems_version: 1.3.7
|
74
82
|
signing_key:
|
75
83
|
specification_version: 3
|
76
|
-
summary:
|
84
|
+
summary: remodel variant which uses hashes
|
77
85
|
test_files:
|
78
86
|
- test/helper.rb
|
79
87
|
- test/test_entity.rb
|
88
|
+
- test/test_entity_defaults.rb
|
89
|
+
- test/test_entity_delete.rb
|
80
90
|
- test/test_many_to_many.rb
|
81
91
|
- test/test_many_to_one.rb
|
82
92
|
- test/test_mappers.rb
|
data/.gitignore
DELETED
data/lib/remodel/has_one.rb
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
module Remodel
|
2
|
-
|
3
|
-
# Represents the one-end of a many-to-one or one-to-one association.
|
4
|
-
class HasOne
|
5
|
-
def initialize(this, clazz, key, reverse = nil)
|
6
|
-
@this, @clazz, @key, @reverse = this, clazz, key, reverse
|
7
|
-
@value = Remodel.redis.get(@key)
|
8
|
-
end
|
9
|
-
|
10
|
-
def value
|
11
|
-
@value
|
12
|
-
end
|
13
|
-
|
14
|
-
def add(entity)
|
15
|
-
_add_to_reverse_association_of(entity) if @reverse
|
16
|
-
_add(entity)
|
17
|
-
end
|
18
|
-
|
19
|
-
def remove(entity)
|
20
|
-
_remove_from_reverse_association_of(entity) if @reverse
|
21
|
-
_remove(entity)
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def _add(entity)
|
27
|
-
@value = entity
|
28
|
-
if entity.nil?
|
29
|
-
Remodel.redis.del(@key)
|
30
|
-
else
|
31
|
-
Remodel.redis.set(@key, entity.key)
|
32
|
-
end
|
33
|
-
entity
|
34
|
-
end
|
35
|
-
|
36
|
-
def _remove(entity)
|
37
|
-
_add(nil)
|
38
|
-
end
|
39
|
-
|
40
|
-
def _add_to_reverse_association_of(entity)
|
41
|
-
if entity.send(@reverse).is_a? HasMany
|
42
|
-
entity.send(@reverse).send(:_add, @this)
|
43
|
-
else
|
44
|
-
entity.send("_#{@reverse}").send., @this)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def _remove_from_reverse_association_of(entity)
|
49
|
-
if entity.send(@reverse).is_a? HasMany
|
50
|
-
entity.send(@reverse).send(:_remove, @this)
|
51
|
-
else
|
52
|
-
entity.send("_#{@reverse}=", nil)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
data/remodel.gemspec
DELETED
@@ -1,72 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{remodel}
|
8
|
-
s.version = "0.1.4"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Tim Lossen"]
|
12
|
-
s.date = %q{2010-07-01}
|
13
|
-
s.default_executable = %q{redis-monitor.rb}
|
14
|
-
s.description = %q{build your domain model in ruby, persist your objects to redis.}
|
15
|
-
s.email = %q{tim@lossen.de}
|
16
|
-
s.executables = ["redis-monitor.rb"]
|
17
|
-
s.extra_rdoc_files = [
|
18
|
-
"LICENSE",
|
19
|
-
"README.md"
|
20
|
-
]
|
21
|
-
s.files = [
|
22
|
-
".gitignore",
|
23
|
-
"LICENSE",
|
24
|
-
"README.md",
|
25
|
-
"Rakefile",
|
26
|
-
"VERSION",
|
27
|
-
"bin/redis-monitor.rb",
|
28
|
-
"docs/docco.css",
|
29
|
-
"docs/remodel.html",
|
30
|
-
"example/book.rb",
|
31
|
-
"lib/remodel.rb",
|
32
|
-
"lib/remodel/entity.rb",
|
33
|
-
"lib/remodel/has_many.rb",
|
34
|
-
"lib/remodel/has_one.rb",
|
35
|
-
"lib/remodel/mapper.rb",
|
36
|
-
"remodel.gemspec",
|
37
|
-
"test/helper.rb",
|
38
|
-
"test/test_entity.rb",
|
39
|
-
"test/test_many_to_many.rb",
|
40
|
-
"test/test_many_to_one.rb",
|
41
|
-
"test/test_mappers.rb",
|
42
|
-
"test/test_monkeypatches.rb",
|
43
|
-
"test/test_one_to_many.rb",
|
44
|
-
"test/test_one_to_one.rb"
|
45
|
-
]
|
46
|
-
s.homepage = %q{http://github.com/tlossen/remodel}
|
47
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
48
|
-
s.require_paths = ["lib"]
|
49
|
-
s.rubygems_version = %q{1.3.5}
|
50
|
-
s.summary = %q{a minimal ORM (object-redis-mapper)}
|
51
|
-
s.test_files = [
|
52
|
-
"test/helper.rb",
|
53
|
-
"test/test_entity.rb",
|
54
|
-
"test/test_many_to_many.rb",
|
55
|
-
"test/test_many_to_one.rb",
|
56
|
-
"test/test_mappers.rb",
|
57
|
-
"test/test_monkeypatches.rb",
|
58
|
-
"test/test_one_to_many.rb",
|
59
|
-
"test/test_one_to_one.rb"
|
60
|
-
]
|
61
|
-
|
62
|
-
if s.respond_to? :specification_version then
|
63
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
64
|
-
s.specification_version = 3
|
65
|
-
|
66
|
-
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
67
|
-
else
|
68
|
-
end
|
69
|
-
else
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|