hashrocket-mongomapper 0.3.7 → 0.3.8

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.7
1
+ 0.3.8
@@ -5,6 +5,51 @@ module MongoMapper
5
5
  @_values = v.map { |e| e.kind_of?(EmbeddedDocument) ? e.attributes : e }
6
6
  reset
7
7
  end
8
+
9
+ def build(opts={})
10
+ owner = @owner
11
+ child = @association.klass.new(opts)
12
+ child.class_eval do
13
+ define_method(owner.class.name.underscore) do
14
+ owner
15
+ end
16
+ end
17
+ child._parent_document = owner
18
+ self << child
19
+ child
20
+ end
21
+
22
+ def find(opts)
23
+ case opts
24
+ when :all
25
+ self
26
+ when String
27
+ if load_target
28
+ child = @target.detect {|item| item.id == opts}
29
+ if child
30
+ owner = @owner
31
+ child.class_eval do
32
+ define_method(owner.class.name.underscore) do
33
+ owner
34
+ end
35
+ end
36
+ end
37
+ child
38
+ end
39
+ end
40
+ end
41
+
42
+ def <<(*docs)
43
+ if load_target
44
+ parent = @owner._parent_document || @owner
45
+ docs.each do |doc|
46
+ doc._parent_document = parent
47
+ @target << doc
48
+ end
49
+ end
50
+ end
51
+ alias_method :push, :<<
52
+ alias_method :concat, :<<
8
53
 
9
54
  protected
10
55
  def find_target
@@ -13,10 +13,11 @@ module MongoMapper
13
13
  include RailsCompatibility::EmbeddedDocument
14
14
  include Validatable
15
15
  include Serialization
16
-
16
+
17
17
  extend Validations::Macros
18
-
18
+
19
19
  key :_id, String
20
+ attr_accessor :_parent_document
20
21
  end
21
22
  end
22
23
 
@@ -43,15 +44,15 @@ module MongoMapper
43
44
 
44
45
  def key(*args)
45
46
  key = Key.new(*args)
46
-
47
+
47
48
  if keys[key.name].blank?
48
49
  keys[key.name] = key
49
-
50
+
50
51
  create_accessors_for(key)
51
52
  add_to_subclasses(*args)
52
53
  apply_validations_for(key)
53
54
  create_indexes_for(key)
54
-
55
+
55
56
  key
56
57
  end
57
58
  end
@@ -88,7 +89,7 @@ module MongoMapper
88
89
  def accessors_module
89
90
  if const_defined?('MongoMapperKeys') && constants.include?( 'MongoMapperKeys' )
90
91
  const_get 'MongoMapperKeys'
91
- else
92
+ else
92
93
  const_set 'MongoMapperKeys', Module.new
93
94
  end
94
95
  end
@@ -113,7 +114,7 @@ module MongoMapper
113
114
  end_eval
114
115
  include accessors_module
115
116
  end
116
-
117
+
117
118
  def create_indexes_for(key)
118
119
  ensure_index key.name if key.options[:index]
119
120
  end
@@ -157,6 +158,12 @@ module MongoMapper
157
158
  unless attrs.nil?
158
159
  self.class.associations.each_pair do |name, association|
159
160
  if collection = attrs.delete(name)
161
+ if association.many? && association.klass.embeddable?
162
+ parent_document = attrs[:_parent_document] || self
163
+ collection.each do |doc|
164
+ doc[:_parent_document] = parent_document
165
+ end
166
+ end
160
167
  send("#{association.name}=", collection)
161
168
  end
162
169
  end
@@ -164,16 +171,23 @@ module MongoMapper
164
171
  self.attributes = attrs
165
172
  end
166
173
 
167
- if self.class.embeddable? && read_attribute(:_id).blank?
168
- write_attribute :_id, XGen::Mongo::Driver::ObjectID.new.to_s
174
+ if self.class.embeddable?
175
+ if read_attribute(:_id).blank?
176
+ write_attribute :_id, XGen::Mongo::Driver::ObjectID.new.to_s
177
+ @new_record = true
178
+ end
169
179
  end
170
180
  end
181
+
182
+ def new_record?
183
+ !!@new_record
184
+ end
171
185
 
172
186
  def attributes=(attrs)
173
187
  return if attrs.blank?
174
188
  attrs.each_pair do |name, value|
175
189
  writer_method = "#{name}="
176
-
190
+
177
191
  if respond_to?(writer_method)
178
192
  self.send(writer_method, value)
179
193
  else
@@ -185,7 +199,7 @@ module MongoMapper
185
199
  def attributes
186
200
  attrs = HashWithIndifferentAccess.new
187
201
  self.class.keys.each_pair do |name, key|
188
- value =
202
+ value =
189
203
  if key.native?
190
204
  read_attribute(key.name)
191
205
  else
@@ -193,33 +207,12 @@ module MongoMapper
193
207
  embedded_document.attributes
194
208
  end
195
209
  end
196
-
197
- attrs[name] = value unless value.nil?
198
- end
199
- attrs.merge!(embedded_association_attributes)
200
- end
201
210
 
202
- def mongodb_attributes
203
- attrs = HashWithIndifferentAccess.new
204
- self.class.keys.each_pair do |name, key|
205
- value =
206
- if key.native?
207
- if key.type == Date and date = instance_variable_get("@#{key.name}")
208
- key.normalize_date(date)
209
- else
210
- read_attribute(key.name)
211
- end
212
- else
213
- if embedded_document = read_attribute(key.name)
214
- embedded_document.mongodb_attributes
215
- end
216
- end
217
-
218
211
  attrs[name] = value unless value.nil?
219
212
  end
220
213
  attrs.merge!(embedded_association_attributes)
221
214
  end
222
-
215
+
223
216
  def [](name)
224
217
  read_attribute(name)
225
218
  end
@@ -241,7 +234,7 @@ module MongoMapper
241
234
  @using_custom_id = true
242
235
  write_attribute :_id, value
243
236
  end
244
-
237
+
245
238
  def using_custom_id?
246
239
  !!@using_custom_id
247
240
  end
@@ -254,6 +247,29 @@ module MongoMapper
254
247
  end
255
248
 
256
249
  private
250
+
251
+ def mongodb_attributes
252
+ attrs = HashWithIndifferentAccess.new
253
+ self.class.keys.each_pair do |name, key|
254
+ value =
255
+ if key.native?
256
+ if key.type == Date and date = instance_variable_get("@#{key.name}")
257
+ key.normalize_date(date)
258
+ else
259
+ read_attribute(key.name)
260
+ end
261
+ else
262
+ if embedded_document = read_attribute(key.name)
263
+ embedded_document.send :mongodb_attributes
264
+ end
265
+ end
266
+
267
+ attrs[name] = value unless value.nil?
268
+ end
269
+ @new_record = false
270
+ attrs.merge!(embedded_association_attributes)
271
+ end
272
+
257
273
  def ensure_key_exists(name)
258
274
  self.class.key(name) unless respond_to?("#{name}=")
259
275
  end
data/mongomapper.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongomapper}
8
- s.version = "0.3.7"
8
+ s.version = "0.3.8"
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{2009-09-03}
12
+ s.date = %q{2009-09-05}
13
13
  s.default_executable = %q{mmconsole}
14
14
  s.email = %q{nunemaker@gmail.com}
15
15
  s.executables = ["mmconsole"]
@@ -9,13 +9,20 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
9
9
  should "default reader to empty array" do
10
10
  Project.new.addresses.should == []
11
11
  end
12
-
12
+
13
13
  should "allow adding to association like it was an array" do
14
14
  project = Project.new
15
15
  project.addresses << Address.new
16
16
  project.addresses.push Address.new
17
17
  project.addresses.size.should == 2
18
18
  end
19
+
20
+ should "allow finding :all embedded documents" do
21
+ project = Project.new
22
+ project.addresses << Address.new
23
+ project.addresses << Address.new
24
+ project.save
25
+ end
19
26
 
20
27
  should "be embedded in document on save" do
21
28
  sb = Address.new(:city => 'South Bend', :state => 'IN')
@@ -30,36 +37,36 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
30
37
  from_db.addresses[0].should == sb
31
38
  from_db.addresses[1].should == chi
32
39
  end
33
-
40
+
34
41
  should "allow embedding arbitrarily deep" do
35
42
  @document = Class.new do
36
43
  include MongoMapper::Document
37
44
  key :person, Person
38
45
  end
39
46
  @document.collection.clear
40
-
47
+
41
48
  meg = Person.new(:name => "Meg")
42
49
  meg.child = Person.new(:name => "Steve")
43
50
  meg.child.child = Person.new(:name => "Linda")
44
-
51
+
45
52
  doc = @document.new(:person => meg)
46
53
  doc.save
47
-
54
+
48
55
  from_db = @document.find(doc.id)
49
56
  from_db.person.name.should == 'Meg'
50
57
  from_db.person.child.name.should == 'Steve'
51
58
  from_db.person.child.child.name.should == 'Linda'
52
59
  end
53
-
60
+
54
61
  should "allow assignment of 'many' embedded documents using a hash" do
55
- person_attributes = {
56
- "name" => "Mr. Pet Lover",
62
+ person_attributes = {
63
+ "name" => "Mr. Pet Lover",
57
64
  "pets" => [
58
65
  {"name" => "Jimmy", "species" => "Cocker Spainel"},
59
- {"name" => "Sasha", "species" => "Siberian Husky"},
60
- ]
66
+ {"name" => "Sasha", "species" => "Siberian Husky"},
67
+ ]
61
68
  }
62
-
69
+
63
70
  pet_lover = RealPerson.new(person_attributes)
64
71
  pet_lover.name.should == "Mr. Pet Lover"
65
72
  pet_lover.pets[0].name.should == "Jimmy"
@@ -67,7 +74,7 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
67
74
  pet_lover.pets[1].name.should == "Sasha"
68
75
  pet_lover.pets[1].species.should == "Siberian Husky"
69
76
  pet_lover.save.should be_true
70
-
77
+
71
78
  from_db = RealPerson.find(pet_lover.id)
72
79
  from_db.name.should == "Mr. Pet Lover"
73
80
  from_db.pets[0].name.should == "Jimmy"
@@ -75,32 +82,90 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
75
82
  from_db.pets[1].name.should == "Sasha"
76
83
  from_db.pets[1].species.should == "Siberian Husky"
77
84
  end
78
-
79
- should "allow saving embedded documents in 'many' embedded documents" do
80
- @document = Class.new do
81
- include MongoMapper::Document
82
- many :people
85
+
86
+ context "embedding many embedded documents" do
87
+ setup do
88
+ @document = Class.new do
89
+ include MongoMapper::Document
90
+ many :people
91
+ end
92
+ @document.collection.clear
83
93
  end
84
- @document.collection.clear
85
-
94
+
95
+ should "persist all embedded documents" do
96
+ meg = Person.new(:name => "Meg")
97
+ sparky = Pet.new(:name => "Sparky", :species => "Dog")
98
+ koda = Pet.new(:name => "Koda", :species => "Dog")
99
+
100
+ doc = @document.new
101
+
102
+ meg.pets << sparky
103
+ meg.pets << koda
104
+
105
+ doc.people << meg
106
+ doc.save
107
+
108
+ from_db = @document.find(doc.id)
109
+ from_db.people.first.name.should == "Meg"
110
+ from_db.people.first.pets.should_not == []
111
+ from_db.people.first.pets.first.name.should == "Sparky"
112
+ from_db.people.first.pets.first.species.should == "Dog"
113
+ from_db.people.first.pets[1].name.should == "Koda"
114
+ from_db.people.first.pets[1].species.should == "Dog"
115
+ end
116
+
117
+ should "create a reference to the parent document for all embedded documents before save" do
118
+ meg = Person.new(:name => "Meg")
119
+ sparky = Pet.new(:name => "Sparky", :species => "Dog")
120
+
121
+ doc = @document.new
122
+
123
+ doc.people << meg
124
+ meg.pets << sparky
125
+
126
+ doc.people.first._parent_document.should == doc
127
+ doc.people.first.pets.first._parent_document.should == doc
128
+ end
129
+
130
+ should "create properly-named reference to parent document when building off association proxy" do
131
+ person = RealPerson.new
132
+ pet = person.pets.build
133
+ person.should == pet.real_person
134
+ end
135
+
136
+
137
+ should "create a reference to the parent document for all embedded documents" do
138
+ meg = Person.new(:name => "Meg")
139
+ sparky = Pet.new(:name => "Sparky", :species => "Dog")
140
+
141
+ doc = @document.new
142
+
143
+ meg.pets << sparky
144
+
145
+ doc.people << meg
146
+ doc.save
147
+
148
+ from_db = @document.find(doc.id)
149
+ from_db.people.first._parent_document.should == doc
150
+ from_db.people.first.pets.first._parent_document.should == doc
151
+ end
152
+ end
153
+
154
+ should "allow retrieval via find(:all)" do
86
155
  meg = Person.new(:name => "Meg")
87
156
  sparky = Pet.new(:name => "Sparky", :species => "Dog")
88
- koda = Pet.new(:name => "Koda", :species => "Dog")
89
-
90
- doc = @document.new
91
-
157
+
92
158
  meg.pets << sparky
93
- meg.pets << koda
94
159
 
95
- doc.people << meg
96
- doc.save
160
+ meg.pets.find(:all).should include(sparky)
161
+ end
162
+
163
+ should "allow retrieval via find(id)" do
164
+ meg = Person.new(:name => "Meg")
165
+ sparky = Pet.new(:name => "Sparky", :species => "Dog")
166
+
167
+ meg.pets << sparky
97
168
 
98
- from_db = @document.find(doc.id)
99
- from_db.people.first.name.should == "Meg"
100
- from_db.people.first.pets.should_not == []
101
- from_db.people.first.pets.first.name.should == "Sparky"
102
- from_db.people.first.pets.first.species.should == "Dog"
103
- from_db.people.first.pets[1].name.should == "Koda"
104
- from_db.people.first.pets[1].species.should == "Dog"
169
+ meg.pets.find(sparky.id).should == sparky
105
170
  end
106
- end
171
+ end
@@ -151,6 +151,35 @@ class DocumentTest < Test::Unit::TestCase
151
151
  end
152
152
  end
153
153
 
154
+ context "#new_record? for embedded documents" do
155
+ setup do
156
+ @document.class_eval do
157
+ key :foo, Address
158
+ end
159
+ end
160
+
161
+ should "be a new_record until document is saved" do
162
+ address = Address.new(:city => 'South Bend', :state => 'IN')
163
+ doc = @document.new(:foo => address)
164
+ address.new_record?.should == true
165
+ end
166
+
167
+ should "not be a new_record after document is saved" do
168
+ address = Address.new(:city => 'South Bend', :state => 'IN')
169
+ doc = @document.new(:foo => address)
170
+ doc.save
171
+ address.new_record?.should == false
172
+ end
173
+
174
+ should "not be a new_record when document is read back" do
175
+ address = Address.new(:city => 'South Bend', :state => 'IN')
176
+ doc = @document.new(:foo => address)
177
+ doc.save
178
+ read_doc = @document.find(doc.id)
179
+ read_doc.foo.new_record?.should == false
180
+ end
181
+ end
182
+
154
183
  context "Creating a single document" do
155
184
  setup do
156
185
  @doc_instance = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@@ -90,6 +90,21 @@ class DocumentTest < Test::Unit::TestCase
90
90
  @document.new(:active => false).active.should be_false
91
91
  end
92
92
 
93
+ context "parent document" do
94
+ should "have a nil _parent_document" do
95
+ @document.new._parent_document.should be_nil
96
+ end
97
+
98
+ should "set self to the parent document on embedded documents" do
99
+ document = Class.new(RealPerson) do
100
+ many :pets
101
+ end
102
+
103
+ doc = document.new 'pets' => [{}]
104
+ doc.pets.first._parent_document.should == doc
105
+ end
106
+ end
107
+
93
108
  context "new?" do
94
109
  should "be true if no id" do
95
110
  @document.new.new?.should be_true
@@ -19,7 +19,7 @@ module KeyOverride
19
19
  def other_child
20
20
  read_attribute(:other_child) || "special result"
21
21
  end
22
-
22
+
23
23
  def other_child=(value)
24
24
  super(value + " modified")
25
25
  end
@@ -28,7 +28,7 @@ end
28
28
  class OtherChild < Parent
29
29
  include MongoMapper::EmbeddedDocument
30
30
  include KeyOverride
31
-
31
+
32
32
  key :other_child, String
33
33
  end
34
34
 
@@ -39,12 +39,12 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
39
39
  include MongoMapper::EmbeddedDocument
40
40
  end
41
41
  end
42
-
42
+
43
43
  should "add _id key" do
44
44
  @klass.keys['_id'].should_not be_nil
45
45
  end
46
46
  end
47
-
47
+
48
48
  context "parent_model" do
49
49
  should "be nil if none of parents ancestors include EmbeddedDocument" do
50
50
  parent = Class.new
@@ -59,53 +59,53 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
59
59
  parent = Class.new grandparent do
60
60
  include MongoMapper::EmbeddedDocument
61
61
  end
62
-
62
+
63
63
  example_module = Module.new
64
64
  document = Class.new(parent) do
65
65
  include MongoMapper::EmbeddedDocument
66
66
  include example_module
67
67
  end
68
-
68
+
69
69
  document.parent_model.should == parent
70
70
  end
71
-
71
+
72
72
  should "find parent" do
73
73
  Parent.parent_model.should == Grandparent
74
74
  Child.parent_model.should == Parent
75
75
  end
76
76
  end
77
-
77
+
78
78
  context "defining a key" do
79
79
  setup do
80
80
  @document = Class.new do
81
81
  include MongoMapper::EmbeddedDocument
82
82
  end
83
83
  end
84
-
84
+
85
85
  should "work with name" do
86
86
  key = @document.key(:name)
87
87
  key.name.should == 'name'
88
88
  end
89
-
89
+
90
90
  should "work with name and type" do
91
91
  key = @document.key(:name, String)
92
92
  key.name.should == 'name'
93
93
  key.type.should == String
94
94
  end
95
-
95
+
96
96
  should "work with name, type and options" do
97
97
  key = @document.key(:name, String, :required => true)
98
98
  key.name.should == 'name'
99
99
  key.type.should == String
100
100
  key.options[:required].should be_true
101
101
  end
102
-
102
+
103
103
  should "work with name and options" do
104
104
  key = @document.key(:name, :required => true)
105
105
  key.name.should == 'name'
106
106
  key.options[:required].should be_true
107
107
  end
108
-
108
+
109
109
  should "be tracked per document" do
110
110
  @document.key(:name, String)
111
111
  @document.key(:age, Integer)
@@ -114,49 +114,49 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
114
114
  @document.keys['age'].name.should == 'age'
115
115
  @document.keys['age'].type.should == Integer
116
116
  end
117
-
117
+
118
118
  should "not be redefinable" do
119
119
  @document.key(:foo, String)
120
120
  @document.keys['foo'].type.should == String
121
121
  @document.key(:foo, Integer)
122
122
  @document.keys['foo'].type.should == String
123
123
  end
124
-
124
+
125
125
  should "create reader method" do
126
126
  @document.new.should_not respond_to(:foo)
127
127
  @document.key(:foo, String)
128
128
  @document.new.should respond_to(:foo)
129
129
  end
130
-
130
+
131
131
  should "create reader before typecast method" do
132
132
  @document.new.should_not respond_to(:foo_before_typecast)
133
133
  @document.key(:foo, String)
134
134
  @document.new.should respond_to(:foo_before_typecast)
135
135
  end
136
-
136
+
137
137
  should "create writer method" do
138
138
  @document.new.should_not respond_to(:foo=)
139
139
  @document.key(:foo, String)
140
140
  @document.new.should respond_to(:foo=)
141
141
  end
142
-
142
+
143
143
  should "create boolean method" do
144
144
  @document.new.should_not respond_to(:foo?)
145
145
  @document.key(:foo, String)
146
146
  @document.new.should respond_to(:foo?)
147
147
  end
148
148
  end
149
-
149
+
150
150
  context "keys" do
151
151
  should "be inherited" do
152
152
  Grandparent.keys.keys.sort.should == ['_id', 'grandparent']
153
153
  Parent.keys.keys.sort.should == ['_id', 'grandparent', 'parent']
154
154
  Child.keys.keys.sort.should == ['_id', 'child', 'grandparent', 'parent']
155
155
  end
156
-
156
+
157
157
  should "propogate to subclasses if key added after class definition" do
158
158
  Grandparent.key :_type, String
159
-
159
+
160
160
  Grandparent.keys.keys.sort.should == ['_id', '_type', 'grandparent']
161
161
  Parent.keys.keys.sort.should == ['_id', '_type', 'grandparent', 'parent']
162
162
  Child.keys.keys.sort.should == ['_id', '_type', 'child', 'grandparent', 'parent']
@@ -185,23 +185,23 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
185
185
  doc.inspect.should include(%(animals: ["dog", "cat"]))
186
186
  end
187
187
  end
188
-
188
+
189
189
  context "subclasses" do
190
190
  should "default to nil" do
191
191
  Child.subclasses.should be_nil
192
192
  end
193
-
193
+
194
194
  should "be recorded" do
195
195
  Grandparent.subclasses.should == [Parent]
196
196
  Parent.subclasses.should == [Child, OtherChild]
197
197
  end
198
198
  end
199
-
199
+
200
200
  context "Applying default values for keys" do
201
201
  setup do
202
202
  @document = Class.new do
203
203
  include MongoMapper::EmbeddedDocument
204
-
204
+
205
205
  key :name, String, :default => 'foo'
206
206
  key :age, Integer, :default => 20
207
207
  key :net_worth, Float, :default => 100.00
@@ -210,33 +210,33 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
210
210
  key :skills, Array, :default => [1]
211
211
  key :options, Hash, :default => {'foo' => 'bar'}
212
212
  end
213
-
213
+
214
214
  @doc = @document.new
215
215
  end
216
-
216
+
217
217
  should "work for strings" do
218
218
  @doc.name.should == 'foo'
219
219
  end
220
-
220
+
221
221
  should "work for integers" do
222
222
  @doc.age.should == 20
223
223
  end
224
-
224
+
225
225
  should "work for floats" do
226
226
  @doc.net_worth.should == 100.00
227
227
  end
228
-
228
+
229
229
  should "work for booleans" do
230
230
  @doc.active.should == true
231
231
  @doc.smart.should == false
232
232
  end
233
-
233
+
234
234
  should "work for arrays" do
235
235
  @doc.skills.should == [1]
236
236
  @doc.skills << 2
237
237
  @doc.skills.should == [1, 2]
238
238
  end
239
-
239
+
240
240
  should "work for hashes" do
241
241
  @doc.options['foo'].should == 'bar'
242
242
  @doc.options['baz'] = 'wick'
@@ -253,22 +253,26 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
253
253
  key :age, Integer
254
254
  end
255
255
  end
256
-
256
+
257
257
  should "automatically have an _id key" do
258
258
  @document.keys.keys.should include('_id')
259
259
  end
260
-
260
+
261
261
  should "have id method that sets _id" do
262
262
  doc = @document.new
263
263
  doc.id.should == doc._id.to_s
264
264
  end
265
-
265
+
266
+ should "have a nil _parent_document" do
267
+ @document.new._parent_document.should be_nil
268
+ end
269
+
266
270
  context "setting custom id" do
267
271
  should "set _id" do
268
272
  doc = @document.new(:id => '1234')
269
273
  doc._id.should == '1234'
270
274
  end
271
-
275
+
272
276
  should "know that custom id is set" do
273
277
  doc = @document.new
274
278
  doc.using_custom_id?.should be_false
@@ -284,13 +288,22 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
284
288
  doc.attributes['name'].should == 'John'
285
289
  doc.attributes['age'].should == 23
286
290
  end
287
-
291
+
288
292
  should "be able to assign keys dynamically" do
289
293
  doc = @document.new(:name => 'John', :skills => ['ruby', 'rails'])
290
294
  doc.name.should == 'John'
291
295
  doc.skills.should == ['ruby', 'rails']
292
296
  end
293
297
 
298
+ should "set the parent id on embedded documents" do
299
+ document = Class.new(@document) do
300
+ many :children
301
+ end
302
+
303
+ doc = document.new :_parent_document => 'document', 'children' => [{}]
304
+ doc.children.first._parent_document.should == 'document'
305
+ end
306
+
294
307
  should "not throw error if initialized with nil" do
295
308
  lambda {
296
309
  @document.new(nil)
@@ -345,20 +358,20 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
345
358
  doc.attributes.values.should include('string')
346
359
  end
347
360
  end
348
-
361
+
349
362
  context "key shorcut access" do
350
363
  should "be able to read key with []" do
351
364
  doc = @document.new(:name => 'string')
352
365
  doc[:name].should == 'string'
353
366
  end
354
-
367
+
355
368
  context "[]=" do
356
369
  should "write key value for existing key" do
357
370
  doc = @document.new
358
371
  doc[:name] = 'string'
359
372
  doc[:name].should == 'string'
360
373
  end
361
-
374
+
362
375
  should "create key and write value for missing key" do
363
376
  doc = @document.new
364
377
  doc[:foo] = 'string'
@@ -367,7 +380,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
367
380
  end
368
381
  end
369
382
  end
370
-
383
+
371
384
  context "indifferent access" do
372
385
  should "be enabled for keys" do
373
386
  doc = @document.new(:name => 'string')
@@ -397,7 +410,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
397
410
  doc = @document.new(:name => 'John', :age => 27)
398
411
  doc.name_and_age.should == 'John (27)'
399
412
  end
400
-
413
+
401
414
  should "set instance variable" do
402
415
  @document.key :foo, Array
403
416
  doc = @document.new
@@ -405,7 +418,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
405
418
  doc.foo
406
419
  doc.instance_variable_get("@foo").should == []
407
420
  end
408
-
421
+
409
422
  should "not set instance variable if frozen" do
410
423
  @document.key :foo, Array
411
424
  doc = @document.new
@@ -414,18 +427,18 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
414
427
  doc.foo
415
428
  doc.instance_variable_get("@foo").should be_nil
416
429
  end
417
-
430
+
418
431
  should "be overrideable by modules" do
419
432
  @document = Class.new do
420
433
  include MongoMapper::Document
421
434
  key :other_child, String
422
435
  end
423
-
436
+
424
437
  child = @document.new
425
438
  child.other_child.should be_nil
426
-
439
+
427
440
  @document.send :include, KeyOverride
428
-
441
+
429
442
  overriden_child = @document.new
430
443
  overriden_child.other_child.should == 'special result'
431
444
  end
@@ -474,7 +487,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
474
487
  doc.age = '21'
475
488
  doc.age.should == 21
476
489
  end
477
-
490
+
478
491
  should "be accessible for use in the model" do
479
492
  @document.class_eval do
480
493
  def name_and_age=(new_value)
@@ -489,23 +502,23 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
489
502
  doc.name.should == 'Frank'
490
503
  doc.age.should == 62
491
504
  end
492
-
505
+
493
506
  should "be overrideable by modules" do
494
507
  @document = Class.new do
495
508
  include MongoMapper::Document
496
509
  key :other_child, String
497
510
  end
498
-
511
+
499
512
  child = @document.new(:other_child => 'foo')
500
513
  child.other_child.should == 'foo'
501
-
514
+
502
515
  @document.send :include, KeyOverride
503
-
516
+
504
517
  overriden_child = @document.new(:other_child => 'foo')
505
518
  overriden_child.other_child.should == 'foo modified'
506
519
  end
507
520
  end # writing an attribute
508
-
521
+
509
522
  context "checking if an attributes value is present" do
510
523
  should "work for defined keys" do
511
524
  doc = @document.new
@@ -513,13 +526,13 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
513
526
  doc.name = 'John'
514
527
  doc.name?.should be_true
515
528
  end
516
-
529
+
517
530
  should "raise no method error for undefined keys" do
518
531
  doc = @document.new
519
532
  lambda { doc.fart? }.should raise_error(NoMethodError)
520
533
  end
521
534
  end
522
-
535
+
523
536
  context "equality" do
524
537
  should "be equal if id and class are the same" do
525
538
  (@document.new('_id' => 1) == @document.new('_id' => 1)).should be(true)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashrocket-mongomapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.3.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-03 00:00:00 -07:00
12
+ date: 2009-09-05 00:00:00 -07:00
13
13
  default_executable: mmconsole
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -135,6 +135,7 @@ files:
135
135
  - test/unit/test_validations.rb
136
136
  has_rdoc: false
137
137
  homepage: http://github.com/jnunemaker/mongomapper
138
+ licenses:
138
139
  post_install_message:
139
140
  rdoc_options:
140
141
  - --charset=UTF-8
@@ -155,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
156
  requirements: []
156
157
 
157
158
  rubyforge_project: mongomapper
158
- rubygems_version: 1.2.0
159
+ rubygems_version: 1.3.5
159
160
  signing_key:
160
161
  specification_version: 3
161
162
  summary: Awesome gem for modeling your domain and storing it in mongo