mongo_mapper 0.7.3 → 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/Rakefile +3 -2
  2. data/lib/mongo_mapper.rb +2 -3
  3. data/lib/mongo_mapper/plugins/associations.rb +10 -1
  4. data/lib/mongo_mapper/plugins/associations/base.rb +2 -2
  5. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +12 -3
  6. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +41 -0
  7. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +1 -0
  8. data/lib/mongo_mapper/plugins/associations/proxy.rb +8 -2
  9. data/lib/mongo_mapper/plugins/callbacks.rb +7 -3
  10. data/lib/mongo_mapper/plugins/descendants.rb +2 -2
  11. data/lib/mongo_mapper/plugins/keys.rb +14 -7
  12. data/lib/mongo_mapper/plugins/modifiers.rb +30 -14
  13. data/lib/mongo_mapper/plugins/protected.rb +1 -1
  14. data/lib/mongo_mapper/plugins/serialization.rb +1 -1
  15. data/lib/mongo_mapper/query.rb +27 -19
  16. data/lib/mongo_mapper/support.rb +10 -6
  17. data/lib/mongo_mapper/version.rb +1 -1
  18. data/mongo_mapper.gemspec +14 -8
  19. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +1 -1
  20. data/test/functional/associations/test_belongs_to_proxy.rb +1 -1
  21. data/test/functional/associations/test_many_documents_proxy.rb +100 -17
  22. data/test/functional/associations/test_one_embedded_proxy.rb +68 -0
  23. data/test/functional/associations/test_one_proxy.rb +48 -13
  24. data/test/functional/test_binary.rb +1 -1
  25. data/test/functional/test_document.rb +7 -7
  26. data/test/functional/test_embedded_document.rb +8 -0
  27. data/test/functional/test_identity_map.rb +2 -2
  28. data/test/functional/test_modifiers.rb +249 -185
  29. data/test/functional/test_protected.rb +4 -0
  30. data/test/functional/test_string_id_compatibility.rb +1 -1
  31. data/test/support/custom_matchers.rb +0 -18
  32. data/test/test_helper.rb +6 -4
  33. data/test/unit/associations/test_base.rb +7 -2
  34. data/test/unit/test_document.rb +5 -5
  35. data/test/unit/test_embedded_document.rb +7 -7
  36. data/test/unit/test_query.rb +17 -7
  37. data/test/unit/test_support.rb +26 -14
  38. metadata +33 -16
@@ -11,10 +11,10 @@ end
11
11
 
12
12
  class Binary
13
13
  def self.to_mongo(value)
14
- if value.is_a?(ByteBuffer)
14
+ if value.is_a?(BSON::Binary)
15
15
  value
16
16
  else
17
- value.nil? ? nil : ByteBuffer.new(value)
17
+ value.nil? ? nil : BSON::Binary.new(value)
18
18
  end
19
19
  end
20
20
 
@@ -129,10 +129,10 @@ class ObjectId
129
129
  def self.to_mongo(value)
130
130
  if value.blank?
131
131
  nil
132
- elsif value.is_a?(Mongo::ObjectID)
132
+ elsif value.is_a?(BSON::ObjectID)
133
133
  value
134
134
  else
135
- Mongo::ObjectID.from_string(value.to_s)
135
+ BSON::ObjectID.from_string(value.to_s)
136
136
  end
137
137
  end
138
138
 
@@ -198,11 +198,15 @@ class Time
198
198
  end
199
199
  end
200
200
 
201
- class Mongo::ObjectID
201
+ class BSON::ObjectID
202
202
  alias_method :original_to_json, :to_json
203
203
 
204
+ def as_json(options=nil)
205
+ to_s
206
+ end
207
+
204
208
  def to_json(options = nil)
205
- %Q("#{to_s}")
209
+ as_json.to_json
206
210
  end
207
211
  end
208
212
 
@@ -1,3 +1,3 @@
1
1
  module MongoMapper
2
- Version = '0.7.3'
2
+ Version = '0.7.4'
3
3
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongo_mapper}
8
- s.version = "0.7.3"
8
+ s.version = "0.7.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Nunemaker"]
12
- s.date = %q{2010-04-05}
12
+ s.date = %q{2010-04-18}
13
13
  s.default_executable = %q{mmconsole}
14
14
  s.email = %q{nunemaker@gmail.com}
15
15
  s.executables = ["mmconsole"]
@@ -39,6 +39,7 @@ Gem::Specification.new do |s|
39
39
  "lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb",
40
40
  "lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb",
41
41
  "lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb",
42
+ "lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb",
42
43
  "lib/mongo_mapper/plugins/associations/one_proxy.rb",
43
44
  "lib/mongo_mapper/plugins/associations/proxy.rb",
44
45
  "lib/mongo_mapper/plugins/callbacks.rb",
@@ -78,6 +79,7 @@ Gem::Specification.new do |s|
78
79
  "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
79
80
  "test/functional/associations/test_many_embedded_proxy.rb",
80
81
  "test/functional/associations/test_many_polymorphic_proxy.rb",
82
+ "test/functional/associations/test_one_embedded_proxy.rb",
81
83
  "test/functional/associations/test_one_proxy.rb",
82
84
  "test/functional/test_associations.rb",
83
85
  "test/functional/test_binary.rb",
@@ -133,6 +135,7 @@ Gem::Specification.new do |s|
133
135
  "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
134
136
  "test/functional/associations/test_many_embedded_proxy.rb",
135
137
  "test/functional/associations/test_many_polymorphic_proxy.rb",
138
+ "test/functional/associations/test_one_embedded_proxy.rb",
136
139
  "test/functional/associations/test_one_proxy.rb",
137
140
  "test/functional/test_associations.rb",
138
141
  "test/functional/test_binary.rb",
@@ -180,16 +183,18 @@ Gem::Specification.new do |s|
180
183
 
181
184
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
182
185
  s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
183
- s.add_runtime_dependency(%q<mongo>, ["= 0.19.3"])
184
- s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.3"])
186
+ s.add_runtime_dependency(%q<mongo>, ["= 0.20.1"])
187
+ s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.4"])
188
+ s.add_development_dependency(%q<json>, [">= 1.2.3"])
185
189
  s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
186
190
  s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
187
191
  s.add_development_dependency(%q<timecop>, ["= 0.3.1"])
188
192
  s.add_development_dependency(%q<mocha>, ["= 0.9.8"])
189
193
  else
190
194
  s.add_dependency(%q<activesupport>, [">= 2.3.4"])
191
- s.add_dependency(%q<mongo>, ["= 0.19.3"])
192
- s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.3"])
195
+ s.add_dependency(%q<mongo>, ["= 0.20.1"])
196
+ s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.4"])
197
+ s.add_dependency(%q<json>, [">= 1.2.3"])
193
198
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
194
199
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])
195
200
  s.add_dependency(%q<timecop>, ["= 0.3.1"])
@@ -197,8 +202,9 @@ Gem::Specification.new do |s|
197
202
  end
198
203
  else
199
204
  s.add_dependency(%q<activesupport>, [">= 2.3.4"])
200
- s.add_dependency(%q<mongo>, ["= 0.19.3"])
201
- s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.3"])
205
+ s.add_dependency(%q<mongo>, ["= 0.20.1"])
206
+ s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.4"])
207
+ s.add_dependency(%q<json>, [">= 1.2.3"])
202
208
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
203
209
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])
204
210
  s.add_dependency(%q<timecop>, ["= 0.3.1"])
@@ -60,4 +60,4 @@ class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
60
60
  @status.target.nil?.should be_true
61
61
  end
62
62
  end
63
- end
63
+ end
@@ -52,7 +52,7 @@ class BelongsToProxyTest < Test::Unit::TestCase
52
52
  end
53
53
 
54
54
  should "return nil if id set but document not found" do
55
- id = Mongo::ObjectID.new
55
+ id = BSON::ObjectID.new
56
56
  @comment_class.new(:name => 'Foo', :post_id => id).post.nil?.should be_true
57
57
  end
58
58
 
@@ -5,6 +5,16 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
5
5
  def setup
6
6
  Project.collection.remove
7
7
  Status.collection.remove
8
+
9
+ @pet_class = Doc do
10
+ key :name, String
11
+ key :owner_id, ObjectId
12
+ end
13
+
14
+ @owner_class = Doc do
15
+ key :name, String
16
+ end
17
+ @owner_class.many :pets, :class => @pet_class, :foreign_key => :owner_id, :order => 'name'
8
18
  end
9
19
 
10
20
  should "default reader to empty array" do
@@ -12,6 +22,32 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
12
22
  project.statuses.should == []
13
23
  end
14
24
 
25
+ should "allow assignment of many associated documents using a hash" do
26
+ person_attributes = {
27
+ 'name' => 'Mr. Pet Lover',
28
+ 'pets' => [
29
+ {'name' => 'Jimmy', 'species' => 'Cocker Spainel'},
30
+ {'name' => 'Sasha', 'species' => 'Siberian Husky'},
31
+ ]
32
+ }
33
+
34
+ owner = @owner_class.new(person_attributes)
35
+ owner.name.should == 'Mr. Pet Lover'
36
+ owner.pets[0].name.should == 'Jimmy'
37
+ owner.pets[0].species.should == 'Cocker Spainel'
38
+ owner.pets[1].name.should == 'Sasha'
39
+ owner.pets[1].species.should == 'Siberian Husky'
40
+
41
+ owner.save.should be_true
42
+ owner.reload
43
+
44
+ owner.name.should == 'Mr. Pet Lover'
45
+ owner.pets[0].name.should == 'Jimmy'
46
+ owner.pets[0].species.should == 'Cocker Spainel'
47
+ owner.pets[1].name.should == 'Sasha'
48
+ owner.pets[1].species.should == 'Siberian Husky'
49
+ end
50
+
15
51
  should "allow adding to association like it was an array" do
16
52
  project = Project.new
17
53
  project.statuses << Status.new(:name => 'Foo1!')
@@ -20,26 +56,60 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
20
56
  project.statuses.size.should == 3
21
57
  end
22
58
 
23
- should "be able to replace the association" do
24
- project = Project.new
25
- project.statuses = [Status.new(:name => "ready")]
26
- project.save.should be_true
59
+ context "replacing the association" do
60
+ context "with objects of the class" do
61
+ should "work" do
62
+ project = Project.new
63
+ project.statuses = [Status.new(:name => "ready")]
64
+ project.save.should be_true
65
+
66
+ project.reload
67
+ project.statuses.size.should == 1
68
+ project.statuses[0].name.should == "ready"
69
+ end
70
+ end
71
+
72
+ context "with Hashes" do
73
+ should "convert to objects of the class and work" do
74
+ project = Project.new
75
+ project.statuses = [{ 'name' => 'ready' }]
76
+ project.save.should be_true
27
77
 
28
- project.reload
29
- project.statuses.size.should == 1
30
- project.statuses[0].name.should == "ready"
78
+ project.reload
79
+ project.statuses.size.should == 1
80
+ project.statuses[0].name.should == "ready"
81
+ end
82
+ end
31
83
  end
32
84
 
33
- should "correctly assign foreign key when using <<, push and concat" do
34
- project = Project.new
35
- project.statuses << Status.new(:name => '<<')
36
- project.statuses.push Status.new(:name => 'push')
37
- project.statuses.concat Status.new(:name => 'concat')
38
-
39
- project.reload
40
- project.statuses[0].project_id.should == project.id
41
- project.statuses[1].project_id.should == project.id
42
- project.statuses[2].project_id.should == project.id
85
+ context "using <<, push and concat" do
86
+ context "with objects of the class" do
87
+ should "correctly assign foreign key" do
88
+ project = Project.new
89
+ project.statuses << Status.new(:name => '<<')
90
+ project.statuses.push Status.new(:name => 'push')
91
+ project.statuses.concat Status.new(:name => 'concat')
92
+
93
+ project.reload
94
+ project.statuses[0].project_id.should == project.id
95
+ project.statuses[1].project_id.should == project.id
96
+ project.statuses[2].project_id.should == project.id
97
+ end
98
+ end
99
+
100
+ context "with Hashes" do
101
+ should "correctly convert to objects and assign foreign key" do
102
+ project = Project.new
103
+ project.statuses << { 'name' => '<<' }
104
+ project.statuses.push( { 'name' => 'push' })
105
+ project.statuses.concat({ 'name' => 'concat' })
106
+
107
+ project.reload
108
+ project.statuses[0].project_id.should == project.id
109
+ project.statuses[1].project_id.should == project.id
110
+ project.statuses[2].project_id.should == project.id
111
+ end
112
+ end
43
113
  end
44
114
 
45
115
  context "build" do
@@ -62,6 +132,19 @@ class ManyDocumentsProxyTest < Test::Unit::TestCase
62
132
  status.save!
63
133
  project.statuses.size.should == 1
64
134
  end
135
+
136
+ should "update collection without save" do
137
+ project = Project.create
138
+ project.statuses.build(:name => 'Foo')
139
+ project.statuses.size.should == 1
140
+ end
141
+
142
+ should "save built document when saving parent" do
143
+ project = Project.create
144
+ status = project.statuses.build(:name => 'Foo')
145
+ project.save!
146
+ status.should_not be_new
147
+ end
65
148
  end
66
149
 
67
150
  context "create" do
@@ -0,0 +1,68 @@
1
+ require 'test_helper'
2
+
3
+ class OneEmbeddedProxyTest < Test::Unit::TestCase
4
+ def setup
5
+ @post_class = Doc('Post') do
6
+ key :title, String
7
+ end
8
+ @author_class = EDoc('Author') do
9
+ key :name, String
10
+ embedded_in :post
11
+ end
12
+ end
13
+
14
+ should "default to nil" do
15
+ @post_class.one :author, :class => @author_class
16
+ @post_class.new.author.should be_nil
17
+ end
18
+
19
+ should "be able to build" do
20
+ @post_class.one :author, :class => @author_class
21
+
22
+ post = @post_class.create
23
+ author = post.author.build(:name => "John")
24
+ post.author.should be_instance_of(@author_class)
25
+ post.author.should be_new
26
+ post.author.name.should == 'John'
27
+ post.author.should == author
28
+ post.author.post.should == post
29
+ end
30
+
31
+ should "send object id to target" do
32
+ @post_class.one :author, :class => @author_class
33
+
34
+ post = @post_class.new
35
+ author = @author_class.new(:name => 'Frank')
36
+ post.author = author
37
+
38
+ post.author.object_id.should == post.author.target.object_id
39
+ end
40
+
41
+ should "be able to replace the association" do
42
+ @post_class.one :author, :class => @author_class
43
+
44
+ post = @post_class.new
45
+ author = @author_class.new(:name => 'Frank')
46
+ post.author = author
47
+ post.save
48
+ post.reload
49
+
50
+ post.author.should == author
51
+ post.author.nil?.should be_false
52
+
53
+ new_author = @author_class.new(:name => 'Emily')
54
+ post.author = new_author
55
+ post.author.should == new_author
56
+ end
57
+
58
+ should "have boolean method for testing presence" do
59
+ @post_class.one :author, :class => @author_class
60
+
61
+ post = @post_class.new
62
+ post.author?.should be_false
63
+
64
+ post.author = @author_class.new(:name => 'Frank')
65
+ post.author?.should be_true
66
+ end
67
+
68
+ end
@@ -24,23 +24,57 @@ class OneProxyTest < Test::Unit::TestCase
24
24
 
25
25
  post.author.object_id.should == post.author.target.object_id
26
26
  end
27
-
28
- should "be able to replace the association" do
27
+
28
+ should "allow assignment of associated document using a hash" do
29
29
  @post_class.one :author, :class => @author_class
30
-
31
- post = @post_class.new
32
- author = @author_class.new(:name => 'Frank')
33
- post.author = author
30
+
31
+ post = @post_class.new('author' => { 'name' => 'Frank' })
32
+ post.author.name.should == 'Frank'
33
+
34
+ post.save.should be_true
34
35
  post.reload
35
-
36
- post.author.should == author
37
- post.author.nil?.should be_false
38
-
39
- new_author = @author_class.new(:name => 'Emily')
40
- post.author = new_author
41
- post.author.should == new_author
36
+
37
+ post.author.name.should == 'Frank'
42
38
  end
43
39
 
40
+ context "replacing the association" do
41
+ context "with an object of the class" do
42
+ should "work" do
43
+ @post_class.one :author, :class => @author_class
44
+
45
+ post = @post_class.new
46
+ author = @author_class.new(:name => 'Frank')
47
+ post.author = author
48
+ post.reload
49
+
50
+ post.author.should == author
51
+ post.author.nil?.should be_false
52
+
53
+ new_author = @author_class.new(:name => 'Emily')
54
+ post.author = new_author
55
+ post.author.should == new_author
56
+ end
57
+ end
58
+
59
+ context "with a Hash" do
60
+ should "convert to an object of the class and work" do
61
+ @post_class.one :author, :class => @author_class
62
+
63
+ post = @post_class.new
64
+ author = { 'name' => 'Frank' }
65
+ post.author = author
66
+ post.reload
67
+
68
+ post.author.name.should == 'Frank'
69
+ post.author.nil?.should be_false
70
+
71
+ new_author = { 'name' => 'Emily' }
72
+ post.author = new_author
73
+ post.author.name.should == 'Emily'
74
+ end
75
+ end
76
+ end
77
+
44
78
  should "have boolean method for testing presence" do
45
79
  @post_class.one :author, :class => @author_class
46
80
 
@@ -58,6 +92,7 @@ class OneProxyTest < Test::Unit::TestCase
58
92
  post = @post_class.create
59
93
  author = @author_class.create(:name => 'Frank', :primary => false, :post_id => post.id)
60
94
  primary = @author_class.create(:name => 'Bill', :primary => true, :post_id => post.id)
95
+ post.reload
61
96
  post.author.should == author
62
97
  post.primary_author.should == primary
63
98
  end
@@ -10,7 +10,7 @@ class BinaryTest < Test::Unit::TestCase
10
10
  doc.save
11
11
 
12
12
  doc = doc.reload
13
- doc.contents.to_s.should == ByteBuffer.new('010101').to_s
13
+ doc.contents.to_s.should == BSON::ByteBuffer.new('010101').to_s
14
14
  end
15
15
 
16
16
  context "Saving a document with a blank binary value" do
@@ -142,8 +142,8 @@ class DocumentTest < Test::Unit::TestCase
142
142
  end
143
143
 
144
144
  should "automatically set id" do
145
- @doc_instance.id.should be_instance_of(Mongo::ObjectID)
146
- @doc_instance._id.should be_instance_of(Mongo::ObjectID)
145
+ @doc_instance.id.should be_instance_of(BSON::ObjectID)
146
+ @doc_instance._id.should be_instance_of(BSON::ObjectID)
147
147
  end
148
148
 
149
149
  should "no longer be new?" do
@@ -303,12 +303,12 @@ class DocumentTest < Test::Unit::TestCase
303
303
  end
304
304
 
305
305
  should "compact not found when using find" do
306
- @document.find(@doc1._id, Mongo::ObjectID.new.to_s).should == [@doc1]
306
+ @document.find(@doc1._id, BSON::ObjectID.new.to_s).should == [@doc1]
307
307
  end
308
308
 
309
309
  should "raise error if not all found when using find!" do
310
310
  assert_raises(MongoMapper::DocumentNotFound) do
311
- @document.find!(@doc1._id, Mongo::ObjectID.new.to_s)
311
+ @document.find!(@doc1._id, BSON::ObjectID.new.to_s)
312
312
  end
313
313
  end
314
314
 
@@ -641,7 +641,7 @@ class DocumentTest < Test::Unit::TestCase
641
641
  end
642
642
 
643
643
  should "assign an id for the document" do
644
- @doc.id.should be_instance_of(Mongo::ObjectID)
644
+ @doc.id.should be_instance_of(BSON::ObjectID)
645
645
  end
646
646
 
647
647
  should "save attributes" do
@@ -709,7 +709,7 @@ class DocumentTest < Test::Unit::TestCase
709
709
  end
710
710
 
711
711
  should "assign an id for the document" do
712
- @doc.id.should be_instance_of(Mongo::ObjectID)
712
+ @doc.id.should be_instance_of(BSON::ObjectID)
713
713
  end
714
714
 
715
715
  should "save attributes" do
@@ -1180,7 +1180,7 @@ class DocumentTest < Test::Unit::TestCase
1180
1180
 
1181
1181
  context "database has keys not defined in model" do
1182
1182
  setup do
1183
- @id = Mongo::ObjectID.new
1183
+ @id = BSON::ObjectID.new
1184
1184
  @document.collection.insert({
1185
1185
  :_id => @id,
1186
1186
  :first_name => 'John',