mongoid 0.8.8 → 0.8.9

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.8.8
1
+ 0.8.9
@@ -86,15 +86,8 @@ module Mongoid #:nodoc:
86
86
  # is initialized by setting the has_many to the supplied +Enumerable+
87
87
  # and setting up the parentization.
88
88
  def update(children, parent, options)
89
- parent.attributes.delete(options.name)
90
- klass = options.klass
91
- children.each do |child|
92
- unless child.respond_to?(:parentize)
93
- child = klass.new(child)
94
- end
95
- child.parentize(parent, options.name)
96
- child.notify
97
- end
89
+ parent.remove_attribute(options.name)
90
+ children.assimilate(parent, options)
98
91
  new(parent, options)
99
92
  end
100
93
  end
@@ -39,15 +39,19 @@ module Mongoid #:nodoc:
39
39
 
40
40
  class << self
41
41
  # Perform an update of the relationship of the parent and child. This
42
- # is initialized by setting the has_one to the supplied child.
42
+ # will assimilate the child +Document+ into the parent's object graph.
43
+ #
44
+ # Options:
45
+ #
46
+ # child: The child +Document+ or +Hash+.
47
+ # parent: The parent +Document+ to update.
48
+ # options: The association +Options+
49
+ #
50
+ # Example:
51
+ #
52
+ # <tt>HasOne.update({:first_name => "Hank"}, person, options)</tt>
43
53
  def update(child, parent, options)
44
- unless child.respond_to?(:parentize)
45
- klass = options.klass
46
- child = klass.new(child)
47
- end
48
- child.parentize(parent, options.name)
49
- child.notify
50
- child
54
+ child.assimilate(parent, options)
51
55
  end
52
56
  end
53
57
 
@@ -121,6 +121,26 @@ module Mongoid #:nodoc:
121
121
  other.attributes.except(:modified_at).except(:created_at)
122
122
  end
123
123
 
124
+ # Introduces a child object into the +Document+ object graph. This will
125
+ # set up the relationships between the parent and child and update the
126
+ # attributes of the parent +Document+.
127
+ #
128
+ # Options:
129
+ #
130
+ # parent: The +Document+ to assimilate with.
131
+ # options: The association +Options+ for the child.
132
+ #
133
+ # Example:
134
+ #
135
+ # <tt>name.assimilate(person, options)</tt>
136
+ #
137
+ # Returns: The child +Document+.
138
+ def assimilate(parent, options)
139
+ parentize(parent, options.name)
140
+ notify
141
+ self
142
+ end
143
+
124
144
  # Clone the current +Document+. This will return all attributes with the
125
145
  # exception of the document's id and versions.
126
146
  def clone
@@ -252,6 +272,20 @@ module Mongoid #:nodoc:
252
272
  fields[name].get(@attributes[name])
253
273
  end
254
274
 
275
+ # Remove a value from the +Document+ attributes. If the value does not exist
276
+ # it will fail gracefully.
277
+ #
278
+ # Options:
279
+ #
280
+ # name: The name of the attribute to remove.
281
+ #
282
+ # Example:
283
+ #
284
+ # <tt>person.remove_attribute(:title)</tt>
285
+ def remove_attribute(name)
286
+ @attributes.delete(name)
287
+ end
288
+
255
289
  # Reloads the +Document+ attributes from the database.
256
290
  def reload
257
291
  @attributes = collection.find_one(:_id => id).with_indifferent_access
@@ -1,3 +1,4 @@
1
+ require "mongoid/extensions/array/assimilation"
1
2
  require "mongoid/extensions/array/conversions"
2
3
  require "mongoid/extensions/array/parentization"
3
4
  require "mongoid/extensions/boolean/conversions"
@@ -5,6 +6,7 @@ require "mongoid/extensions/date/conversions"
5
6
  require "mongoid/extensions/datetime/conversions"
6
7
  require "mongoid/extensions/float/conversions"
7
8
  require "mongoid/extensions/hash/accessors"
9
+ require "mongoid/extensions/hash/assimilation"
8
10
  require "mongoid/extensions/hash/conversions"
9
11
  require "mongoid/extensions/integer/conversions"
10
12
  require "mongoid/extensions/object/conversions"
@@ -14,6 +16,7 @@ require "mongoid/extensions/symbol/inflections"
14
16
  require "mongoid/extensions/time/conversions"
15
17
 
16
18
  class Array #:nodoc:
19
+ include Mongoid::Extensions::Array::Assimilation
17
20
  include Mongoid::Extensions::Array::Conversions
18
21
  include Mongoid::Extensions::Array::Parentization
19
22
  end
@@ -36,6 +39,7 @@ end
36
39
 
37
40
  class Hash #:nodoc
38
41
  include Mongoid::Extensions::Hash::Accessors
42
+ include Mongoid::Extensions::Hash::Assimilation
39
43
  extend Mongoid::Extensions::Hash::Conversions
40
44
  end
41
45
 
@@ -0,0 +1,25 @@
1
+ module Mongoid #:nodoc:
2
+ module Extensions #:nodoc:
3
+ module Array #:nodoc:
4
+ module Assimilation #:nodoc:
5
+ # Introduces a child object into the +Document+ object graph. This will
6
+ # set up the relationships between the parent and child and update the
7
+ # attributes of the parent +Document+.
8
+ #
9
+ # Options:
10
+ #
11
+ # parent: The +Document+ to assimilate into.
12
+ # options: The association +Options+ for the child.
13
+ #
14
+ # Example:
15
+ #
16
+ # <tt>[{:street => "Queen St."}, {:street => "King St."}].assimilate(person, options)</tt>
17
+ #
18
+ # Returns: The child +Document+.
19
+ def assimilate(parent, options)
20
+ each { |child| child.assimilate(parent, options) }
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ module Mongoid #:nodoc:
2
+ module Extensions #:nodoc:
3
+ module Hash #:nodoc:
4
+ module Assimilation #:nodoc:
5
+ # Introduces a child object into the +Document+ object graph. This will
6
+ # set up the relationships between the parent and child and update the
7
+ # attributes of the parent +Document+.
8
+ #
9
+ # Options:
10
+ #
11
+ # parent: The +Document+ to assimilate into.
12
+ # options: The association +Options+ for the child.
13
+ #
14
+ # Example:
15
+ #
16
+ # <tt>{:first_name => "Hank", :last_name => "Moody"}.assimilate(name, options)</tt>
17
+ #
18
+ # Returns: The child +Document+.
19
+ def assimilate(parent, options)
20
+ klass = options.klass
21
+ child = klass.new(self)
22
+ child.assimilate(parent, options)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.8.8"
8
+ s.version = "0.8.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Durran Jordan"]
12
- s.date = %q{2009-11-24}
12
+ s.date = %q{2009-11-26}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -41,6 +41,7 @@ Gem::Specification.new do |s|
41
41
  "lib/mongoid/criteria.rb",
42
42
  "lib/mongoid/document.rb",
43
43
  "lib/mongoid/extensions.rb",
44
+ "lib/mongoid/extensions/array/assimilation.rb",
44
45
  "lib/mongoid/extensions/array/conversions.rb",
45
46
  "lib/mongoid/extensions/array/parentization.rb",
46
47
  "lib/mongoid/extensions/boolean/conversions.rb",
@@ -48,6 +49,7 @@ Gem::Specification.new do |s|
48
49
  "lib/mongoid/extensions/datetime/conversions.rb",
49
50
  "lib/mongoid/extensions/float/conversions.rb",
50
51
  "lib/mongoid/extensions/hash/accessors.rb",
52
+ "lib/mongoid/extensions/hash/assimilation.rb",
51
53
  "lib/mongoid/extensions/hash/conversions.rb",
52
54
  "lib/mongoid/extensions/integer/conversions.rb",
53
55
  "lib/mongoid/extensions/object/casting.rb",
@@ -83,6 +85,7 @@ Gem::Specification.new do |s|
83
85
  "spec/unit/mongoid/commands_spec.rb",
84
86
  "spec/unit/mongoid/criteria_spec.rb",
85
87
  "spec/unit/mongoid/document_spec.rb",
88
+ "spec/unit/mongoid/extensions/array/assimilation_spec.rb",
86
89
  "spec/unit/mongoid/extensions/array/conversions_spec.rb",
87
90
  "spec/unit/mongoid/extensions/array/parentization_spec.rb",
88
91
  "spec/unit/mongoid/extensions/boolean/conversions_spec.rb",
@@ -90,6 +93,7 @@ Gem::Specification.new do |s|
90
93
  "spec/unit/mongoid/extensions/datetime/conversions_spec.rb",
91
94
  "spec/unit/mongoid/extensions/float/conversions_spec.rb",
92
95
  "spec/unit/mongoid/extensions/hash/accessors_spec.rb",
96
+ "spec/unit/mongoid/extensions/hash/assimilation_spec.rb",
93
97
  "spec/unit/mongoid/extensions/hash/conversions_spec.rb",
94
98
  "spec/unit/mongoid/extensions/integer/conversions_spec.rb",
95
99
  "spec/unit/mongoid/extensions/object/conversions_spec.rb",
@@ -128,6 +132,7 @@ Gem::Specification.new do |s|
128
132
  "spec/unit/mongoid/commands_spec.rb",
129
133
  "spec/unit/mongoid/criteria_spec.rb",
130
134
  "spec/unit/mongoid/document_spec.rb",
135
+ "spec/unit/mongoid/extensions/array/assimilation_spec.rb",
131
136
  "spec/unit/mongoid/extensions/array/conversions_spec.rb",
132
137
  "spec/unit/mongoid/extensions/array/parentization_spec.rb",
133
138
  "spec/unit/mongoid/extensions/boolean/conversions_spec.rb",
@@ -135,6 +140,7 @@ Gem::Specification.new do |s|
135
140
  "spec/unit/mongoid/extensions/datetime/conversions_spec.rb",
136
141
  "spec/unit/mongoid/extensions/float/conversions_spec.rb",
137
142
  "spec/unit/mongoid/extensions/hash/accessors_spec.rb",
143
+ "spec/unit/mongoid/extensions/hash/assimilation_spec.rb",
138
144
  "spec/unit/mongoid/extensions/hash/conversions_spec.rb",
139
145
  "spec/unit/mongoid/extensions/integer/conversions_spec.rb",
140
146
  "spec/unit/mongoid/extensions/object/conversions_spec.rb",
@@ -56,6 +56,21 @@ describe Mongoid::Document do
56
56
 
57
57
  end
58
58
 
59
+ describe "#assimilate" do
60
+
61
+ before do
62
+ @child = Name.new(:first_name => "Hank", :last_name => "Moody")
63
+ @parent = Person.new(:title => "Mr.")
64
+ @options = Mongoid::Associations::Options.new(:name => :name)
65
+ end
66
+
67
+ it "sets up all associations in the object graph" do
68
+ @child.assimilate(@parent, @options)
69
+ @parent.name.should == @child
70
+ end
71
+
72
+ end
73
+
59
74
  describe "#clone" do
60
75
 
61
76
  before do
@@ -445,6 +460,30 @@ describe Mongoid::Document do
445
460
 
446
461
  end
447
462
 
463
+ describe "#remove_attribute" do
464
+
465
+ context "when the attribute exists" do
466
+
467
+ it "removes the attribute" do
468
+ person = Person.new(:title => "Sir")
469
+ person.remove_attribute(:title)
470
+ person.title.should be_nil
471
+ end
472
+
473
+ end
474
+
475
+ context "when the attribute does not exist" do
476
+
477
+ it "does nothing" do
478
+ person = Person.new
479
+ person.remove_attribute(:title)
480
+ person.title.should be_nil
481
+ end
482
+
483
+ end
484
+
485
+ end
486
+
448
487
  describe "#reload" do
449
488
 
450
489
  before do
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb"))
2
+
3
+ describe Mongoid::Extensions::Array::Assimilation do
4
+
5
+ describe "#assimilate" do
6
+
7
+ before do
8
+ @address_one = { :street => "Circular Quay" }
9
+ @address_two = Address.new(:street => "King St.")
10
+ @parent = Person.new(:title => "Mr.")
11
+ @options = Mongoid::Associations::Options.new(:name => :addresses)
12
+ @child = [@address_one, @address_two]
13
+ end
14
+
15
+ it "incorporates the hash into the object graph" do
16
+ @child.assimilate(@parent, @options)
17
+ @parent.addresses.size.should == 2
18
+ @parent.addresses.first.street.should == "Circular Quay"
19
+ @parent.addresses.last.street.should == "King St."
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,21 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb"))
2
+
3
+ describe Mongoid::Extensions::Hash::Assimilation do
4
+
5
+ describe "#assimilate" do
6
+
7
+ before do
8
+ @child = { :first_name => "Hank", :last_name => "Moody" }
9
+ @parent = Person.new(:title => "Mr.")
10
+ @options = Mongoid::Associations::Options.new(:name => :name)
11
+ end
12
+
13
+ it "incorporates the hash into the object graph" do
14
+ @child.assimilate(@parent, @options)
15
+ @parent.name.first_name.should == "Hank"
16
+ @parent.name.last_name.should == "Moody"
17
+ end
18
+
19
+ end
20
+
21
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.8
4
+ version: 0.8.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-24 00:00:00 -05:00
12
+ date: 2009-11-26 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -97,6 +97,7 @@ files:
97
97
  - lib/mongoid/criteria.rb
98
98
  - lib/mongoid/document.rb
99
99
  - lib/mongoid/extensions.rb
100
+ - lib/mongoid/extensions/array/assimilation.rb
100
101
  - lib/mongoid/extensions/array/conversions.rb
101
102
  - lib/mongoid/extensions/array/parentization.rb
102
103
  - lib/mongoid/extensions/boolean/conversions.rb
@@ -104,6 +105,7 @@ files:
104
105
  - lib/mongoid/extensions/datetime/conversions.rb
105
106
  - lib/mongoid/extensions/float/conversions.rb
106
107
  - lib/mongoid/extensions/hash/accessors.rb
108
+ - lib/mongoid/extensions/hash/assimilation.rb
107
109
  - lib/mongoid/extensions/hash/conversions.rb
108
110
  - lib/mongoid/extensions/integer/conversions.rb
109
111
  - lib/mongoid/extensions/object/casting.rb
@@ -139,6 +141,7 @@ files:
139
141
  - spec/unit/mongoid/commands_spec.rb
140
142
  - spec/unit/mongoid/criteria_spec.rb
141
143
  - spec/unit/mongoid/document_spec.rb
144
+ - spec/unit/mongoid/extensions/array/assimilation_spec.rb
142
145
  - spec/unit/mongoid/extensions/array/conversions_spec.rb
143
146
  - spec/unit/mongoid/extensions/array/parentization_spec.rb
144
147
  - spec/unit/mongoid/extensions/boolean/conversions_spec.rb
@@ -146,6 +149,7 @@ files:
146
149
  - spec/unit/mongoid/extensions/datetime/conversions_spec.rb
147
150
  - spec/unit/mongoid/extensions/float/conversions_spec.rb
148
151
  - spec/unit/mongoid/extensions/hash/accessors_spec.rb
152
+ - spec/unit/mongoid/extensions/hash/assimilation_spec.rb
149
153
  - spec/unit/mongoid/extensions/hash/conversions_spec.rb
150
154
  - spec/unit/mongoid/extensions/integer/conversions_spec.rb
151
155
  - spec/unit/mongoid/extensions/object/conversions_spec.rb
@@ -206,6 +210,7 @@ test_files:
206
210
  - spec/unit/mongoid/commands_spec.rb
207
211
  - spec/unit/mongoid/criteria_spec.rb
208
212
  - spec/unit/mongoid/document_spec.rb
213
+ - spec/unit/mongoid/extensions/array/assimilation_spec.rb
209
214
  - spec/unit/mongoid/extensions/array/conversions_spec.rb
210
215
  - spec/unit/mongoid/extensions/array/parentization_spec.rb
211
216
  - spec/unit/mongoid/extensions/boolean/conversions_spec.rb
@@ -213,6 +218,7 @@ test_files:
213
218
  - spec/unit/mongoid/extensions/datetime/conversions_spec.rb
214
219
  - spec/unit/mongoid/extensions/float/conversions_spec.rb
215
220
  - spec/unit/mongoid/extensions/hash/accessors_spec.rb
221
+ - spec/unit/mongoid/extensions/hash/assimilation_spec.rb
216
222
  - spec/unit/mongoid/extensions/hash/conversions_spec.rb
217
223
  - spec/unit/mongoid/extensions/integer/conversions_spec.rb
218
224
  - spec/unit/mongoid/extensions/object/conversions_spec.rb