mongoid 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.0
1
+ 0.7.1
@@ -21,7 +21,7 @@ module Mongoid # :nodoc:
21
21
 
22
22
  module ClassMethods
23
23
  def associations
24
- @associations ||= HashWithIndifferentAccess.new
24
+ @associations ||= {}.with_indifferent_access
25
25
  end
26
26
  # Adds the association back to the parent document. This macro is
27
27
  # necessary to set the references from the child back to the parent
@@ -28,8 +28,11 @@ module Mongoid #:nodoc:
28
28
  def update(parent, child, name, options = {})
29
29
  name = child.class.name.demodulize.downcase
30
30
  has_one = parent.associations[name]
31
- child.parentize(parent, name) if has_one
32
- child.parentize(parent, name.tableize) unless has_one
31
+ if has_one
32
+ child.parentize(parent, name)
33
+ else
34
+ child.parentize(parent, name.tableize)
35
+ end
33
36
  child.notify
34
37
  parent
35
38
  end
@@ -66,7 +66,7 @@ module Mongoid #:nodoc:
66
66
  @klass = class_name ? class_name.constantize : @association_name.to_s.classify.constantize
67
67
  attributes = document.attributes[@association_name]
68
68
  @documents = attributes ? attributes.collect do |attribute|
69
- child = @klass.new(attribute)
69
+ child = @klass.instantiate(attribute)
70
70
  child.parentize(@parent, @association_name)
71
71
  child
72
72
  end : []
@@ -79,7 +79,12 @@ module Mongoid #:nodoc:
79
79
  # and setting up the parentization.
80
80
  def update(children, parent, name, options = {})
81
81
  parent.attributes.delete(name)
82
+ class_name = options[:class_name]
83
+ klass = class_name ? class_name.constantize : name.to_s.classify.constantize
82
84
  children.each do |child|
85
+ unless child.respond_to?(:parentize)
86
+ child = klass.new(child)
87
+ end
83
88
  child.parentize(parent, name)
84
89
  child.notify
85
90
  end
@@ -15,7 +15,7 @@ module Mongoid #:nodoc:
15
15
  class_name = options[:class_name]
16
16
  klass = class_name ? class_name.constantize : name.to_s.camelize.constantize
17
17
  attributes = document.attributes[name]
18
- @document = klass.new(attributes)
18
+ @document = klass.instantiate(attributes || {})
19
19
  @document.parentize(document, name)
20
20
  decorate!
21
21
  end
@@ -24,6 +24,11 @@ module Mongoid #:nodoc:
24
24
  # Perform an update of the relationship of the parent and child. This
25
25
  # is initialized by setting the has_one to the supplied child.
26
26
  def update(child, parent, name, options = {})
27
+ unless child.respond_to?(:parentize)
28
+ class_name = options[:class_name]
29
+ klass = class_name ? class_name.constantize : name.to_s.camelize.constantize
30
+ child = klass.new(child)
31
+ end
27
32
  child.parentize(parent, name)
28
33
  child.notify
29
34
  child
@@ -2,10 +2,10 @@ module Mongoid #:nodoc:
2
2
  module Attributes #:nodoc:
3
3
  # Process the provided attributes casting them to their proper values if a
4
4
  # field exists for them on the +Document+.
5
- def process(params)
6
- @attributes = HashWithIndifferentAccess.new(params)
5
+ def process(params = nil)
6
+ @attributes = (params || {}).with_indifferent_access
7
7
  process_fields
8
- process_associations
8
+ process_attributes
9
9
  end
10
10
 
11
11
  protected
@@ -16,9 +16,9 @@ module Mongoid #:nodoc:
16
16
  end
17
17
  end
18
18
 
19
- def process_associations
19
+ def process_attributes
20
20
  @attributes.each_pair do |key, value|
21
- @attributes[key] = send("#{key}=", value) if value.is_a?(Document)
21
+ send("#{key}=", value)
22
22
  end
23
23
  end
24
24
 
@@ -102,10 +102,10 @@ module Mongoid #:nodoc:
102
102
  @klass = klass if klass
103
103
  if type == :first
104
104
  attributes = klass.collection.find_one(@selector, @options)
105
- attributes ? @klass.new(attributes) : nil
105
+ attributes ? @klass.instantiate(attributes) : nil
106
106
  else
107
107
  attributes = @klass.collection.find(@selector, @options)
108
- attributes ? attributes.collect { |doc| @klass.new(doc) } : []
108
+ attributes ? attributes.collect { |doc| @klass.instantiate(doc) } : []
109
109
  end
110
110
  end
111
111
 
@@ -142,7 +142,7 @@ module Mongoid #:nodoc:
142
142
  { :group => [] },
143
143
  GROUP_REDUCE
144
144
  ).collect do |docs|
145
- docs["group"] = docs["group"].collect { |attrs| @klass.new(attrs) }; docs
145
+ docs["group"] = docs["group"].collect { |attrs| @klass.instantiate(attrs) }; docs
146
146
  end
147
147
  end
148
148
 
@@ -68,7 +68,7 @@ module Mongoid #:nodoc:
68
68
  #
69
69
  # <tt>field :score, :default => 0</tt>
70
70
  def field(name, options = {})
71
- @fields ||= HashWithIndifferentAccess.new
71
+ @fields ||= {}.with_indifferent_access
72
72
  @fields[name.to_s] = Field.new(name.to_s, options)
73
73
  define_method(name) { read_attribute(name) }
74
74
  define_method("#{name}=") { |value| write_attribute(name, value) }
@@ -118,6 +118,13 @@ module Mongoid #:nodoc:
118
118
  collection.create_index(name, options)
119
119
  end
120
120
 
121
+ # Instantiate a new object, only when loaded from the database.
122
+ def instantiate(attributes = {})
123
+ document = allocate
124
+ document.instance_variable_set(:@attributes, attributes.with_indifferent_access)
125
+ document
126
+ end
127
+
121
128
  # Defines the field that will be used for the id of this +Document+. This
122
129
  # set the id of this +Document+ before save to a parameterized version of
123
130
  # the field that was supplied. This is good for use for readable URLS in
@@ -207,7 +214,13 @@ module Mongoid #:nodoc:
207
214
  @attributes[:_id]
208
215
  end
209
216
 
217
+ # Set the id
218
+ def id=(new_id)
219
+ @attributes[:_id] = new_id
220
+ end
221
+
210
222
  alias :_id :id
223
+ alias :_id= :id=
211
224
 
212
225
  # Instantiate a new Document, setting the Document's attributes if given.
213
226
  # If no attributes are provided, they will be initialized with an empty Hash.
@@ -237,6 +250,13 @@ module Mongoid #:nodoc:
237
250
  notify_observers(self)
238
251
  end
239
252
 
253
+ # Sets the parent object
254
+ def parentize(object, association_name)
255
+ self.parent = object
256
+ self.association_name = association_name
257
+ add_observer(object)
258
+ end
259
+
240
260
  # Read from the attributes hash.
241
261
  def read_attribute(name)
242
262
  fields[name].get(@attributes[name])
@@ -244,7 +264,7 @@ module Mongoid #:nodoc:
244
264
 
245
265
  # Reloads the +Document+ attributes from the database.
246
266
  def reload
247
- @attributes = HashWithIndifferentAccess.new(collection.find_one(:_id => id))
267
+ @attributes = collection.find_one(:_id => id).with_indifferent_access
248
268
  end
249
269
 
250
270
  # Return the root +Document+ in the object graph.
@@ -7,7 +7,6 @@ require "mongoid/extensions/hash/accessors"
7
7
  require "mongoid/extensions/hash/conversions"
8
8
  require "mongoid/extensions/integer/conversions"
9
9
  require "mongoid/extensions/object/conversions"
10
- require "mongoid/extensions/object/parentization"
11
10
  require "mongoid/extensions/string/conversions"
12
11
  require "mongoid/extensions/string/inflections"
13
12
  require "mongoid/extensions/symbol/inflections"
@@ -41,7 +40,6 @@ end
41
40
 
42
41
  class Object #:nodoc:
43
42
  include Mongoid::Extensions::Object::Conversions
44
- include Mongoid::Extensions::Object::Parentization
45
43
  end
46
44
 
47
45
  class String #:nodoc
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.7.0"
8
+ s.version = "0.7.1"
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-11}
12
+ s.date = %q{2009-11-12}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -50,7 +50,6 @@ Gem::Specification.new do |s|
50
50
  "lib/mongoid/extensions/integer/conversions.rb",
51
51
  "lib/mongoid/extensions/object/casting.rb",
52
52
  "lib/mongoid/extensions/object/conversions.rb",
53
- "lib/mongoid/extensions/object/parentization.rb",
54
53
  "lib/mongoid/extensions/string/conversions.rb",
55
54
  "lib/mongoid/extensions/string/inflections.rb",
56
55
  "lib/mongoid/extensions/symbol/inflections.rb",
@@ -88,7 +87,6 @@ Gem::Specification.new do |s|
88
87
  "spec/unit/mongoid/extensions/hash/conversions_spec.rb",
89
88
  "spec/unit/mongoid/extensions/integer/conversions_spec.rb",
90
89
  "spec/unit/mongoid/extensions/object/conversions_spec.rb",
91
- "spec/unit/mongoid/extensions/object/parentization_spec.rb",
92
90
  "spec/unit/mongoid/extensions/string/conversions_spec.rb",
93
91
  "spec/unit/mongoid/extensions/string/inflections_spec.rb",
94
92
  "spec/unit/mongoid/extensions/symbol/inflections_spec.rb",
@@ -131,7 +129,6 @@ Gem::Specification.new do |s|
131
129
  "spec/unit/mongoid/extensions/hash/conversions_spec.rb",
132
130
  "spec/unit/mongoid/extensions/integer/conversions_spec.rb",
133
131
  "spec/unit/mongoid/extensions/object/conversions_spec.rb",
134
- "spec/unit/mongoid/extensions/object/parentization_spec.rb",
135
132
  "spec/unit/mongoid/extensions/string/conversions_spec.rb",
136
133
  "spec/unit/mongoid/extensions/string/inflections_spec.rb",
137
134
  "spec/unit/mongoid/extensions/symbol/inflections_spec.rb",
@@ -30,9 +30,9 @@ describe Mongoid::Document do
30
30
  describe "#create" do
31
31
 
32
32
  it "persists a new record to the database" do
33
- person = Person.create(:test => "Test")
33
+ person = Person.create(:title => "Test")
34
34
  person.id.should be_a_kind_of(String)
35
- person.attributes[:test].should == "Test"
35
+ person.attributes[:title].should == "Test"
36
36
  end
37
37
 
38
38
  end
@@ -26,6 +26,7 @@ class Person < Mongoid::Document
26
26
  field :age, :type => Integer, :default => 100
27
27
  field :dob, :type => Date
28
28
  field :mixed_drink, :type => MixedDrink
29
+ field :employer_id
29
30
  has_many :addresses
30
31
  has_many :phone_numbers, :class_name => "Phone"
31
32
  has_one :name
@@ -36,6 +37,16 @@ class Person < Mongoid::Document
36
37
  address.street = "Updated #{i}"
37
38
  end
38
39
  end
40
+
41
+ def employer=(emp)
42
+ self.employer_id = emp.id
43
+ end
44
+ end
45
+
46
+ class Employer
47
+ def id
48
+ "1"
49
+ end
39
50
  end
40
51
 
41
52
  class CountryCode < Mongoid::Document
@@ -78,7 +89,7 @@ class Address < Mongoid::Document
78
89
  field :street
79
90
  field :city
80
91
  field :state
81
- field :comment_code
92
+ field :post_code
82
93
  key :street
83
94
  belongs_to :addressable
84
95
  end
@@ -4,8 +4,8 @@ describe Mongoid::Associations::HasMany do
4
4
 
5
5
  before do
6
6
  @attributes = { :addresses => [
7
- { :street => "Street 1" },
8
- { :street => "Street 2" } ] }
7
+ { :_id => "street-1", :street => "Street 1" },
8
+ { :_id => "street-2", :street => "Street 2" } ] }
9
9
  @document = stub(:attributes => @attributes, :add_observer => true, :update => true)
10
10
  end
11
11
 
@@ -64,6 +64,20 @@ describe Mongoid::Attributes do
64
64
 
65
65
  end
66
66
 
67
+ context "when non-associations provided in the attributes" do
68
+
69
+ before do
70
+ @employer = Employer.new
71
+ @attributes = { :employer => @employer, :title => "Sir" }
72
+ @person = Person.new(@attributes)
73
+ end
74
+
75
+ it "calls the setter for the association" do
76
+ @person.employer_id.should == "1"
77
+ end
78
+
79
+ end
80
+
67
81
  end
68
82
 
69
83
  end
@@ -292,7 +292,7 @@ describe Mongoid::Document do
292
292
  describe "#first" do
293
293
 
294
294
  before do
295
- @attributes = { "age" => 100, "version" => 1 }
295
+ @attributes = { "age" => 100 }
296
296
  end
297
297
 
298
298
  context "when a selector is provided" do
@@ -349,6 +349,19 @@ describe Mongoid::Document do
349
349
 
350
350
  end
351
351
 
352
+ describe "#instantiate" do
353
+
354
+ before do
355
+ @attributes = { :_id => "1", :title => "Sir", :age => 30 }
356
+ @person = Person.new(@attributes)
357
+ end
358
+
359
+ it "sets the attributes directly" do
360
+ Person.instantiate(@attributes).should == @person
361
+ end
362
+
363
+ end
364
+
352
365
  describe "#key" do
353
366
 
354
367
  context "when key is single field" do
@@ -370,8 +383,8 @@ describe Mongoid::Document do
370
383
  context "when key is composite" do
371
384
 
372
385
  before do
373
- Address.key :street, :zip
374
- @address = Address.new(:street => "Testing Street Name", :zip => "94123")
386
+ Address.key :street, :post_code
387
+ @address = Address.new(:street => "Testing Street Name", :post_code => "94123")
375
388
  @address.expects(:collection).returns(@collection)
376
389
  @collection.expects(:save)
377
390
  end
@@ -393,7 +406,7 @@ describe Mongoid::Document do
393
406
  end
394
407
 
395
408
  it "finds the last document by the id" do
396
- Person.last.should == Person.new(@attributes)
409
+ Person.last.should == Person.instantiate(@attributes)
397
410
  end
398
411
 
399
412
  end
@@ -547,6 +560,20 @@ describe Mongoid::Document do
547
560
 
548
561
  end
549
562
 
563
+ describe "#parentize" do
564
+
565
+ before do
566
+ @parent = Person.new
567
+ @child = Name.new
568
+ end
569
+
570
+ it "sets the parent on each element" do
571
+ @child.parentize(@parent, :child)
572
+ @child.parent.should == @parent
573
+ end
574
+
575
+ end
576
+
550
577
  describe "#read_attribute" do
551
578
 
552
579
  context "when attribute does not exist" do
@@ -11,9 +11,7 @@ describe Mongoid::Extensions::Array::Parentization do
11
11
  end
12
12
 
13
13
  it "sets the parent on each element" do
14
- @child.expects(:add_observer).with(@parent)
15
- @child.expects(:parent=).with(@parent)
16
- @child.expects(:association_name=).with(:child)
14
+ @child.expects(:parentize).with(@parent, :child)
17
15
  @array.parentize(@parent, :child)
18
16
  end
19
17
 
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.7.0
4
+ version: 0.7.1
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-11 00:00:00 -05:00
12
+ date: 2009-11-12 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -106,7 +106,6 @@ files:
106
106
  - lib/mongoid/extensions/integer/conversions.rb
107
107
  - lib/mongoid/extensions/object/casting.rb
108
108
  - lib/mongoid/extensions/object/conversions.rb
109
- - lib/mongoid/extensions/object/parentization.rb
110
109
  - lib/mongoid/extensions/string/conversions.rb
111
110
  - lib/mongoid/extensions/string/inflections.rb
112
111
  - lib/mongoid/extensions/symbol/inflections.rb
@@ -144,7 +143,6 @@ files:
144
143
  - spec/unit/mongoid/extensions/hash/conversions_spec.rb
145
144
  - spec/unit/mongoid/extensions/integer/conversions_spec.rb
146
145
  - spec/unit/mongoid/extensions/object/conversions_spec.rb
147
- - spec/unit/mongoid/extensions/object/parentization_spec.rb
148
146
  - spec/unit/mongoid/extensions/string/conversions_spec.rb
149
147
  - spec/unit/mongoid/extensions/string/inflections_spec.rb
150
148
  - spec/unit/mongoid/extensions/symbol/inflections_spec.rb
@@ -209,7 +207,6 @@ test_files:
209
207
  - spec/unit/mongoid/extensions/hash/conversions_spec.rb
210
208
  - spec/unit/mongoid/extensions/integer/conversions_spec.rb
211
209
  - spec/unit/mongoid/extensions/object/conversions_spec.rb
212
- - spec/unit/mongoid/extensions/object/parentization_spec.rb
213
210
  - spec/unit/mongoid/extensions/string/conversions_spec.rb
214
211
  - spec/unit/mongoid/extensions/string/inflections_spec.rb
215
212
  - spec/unit/mongoid/extensions/symbol/inflections_spec.rb
@@ -1,14 +0,0 @@
1
- module Mongoid #:nodoc:
2
- module Extensions #:nodoc:
3
- module Object #:nodoc:
4
- module Parentization #:nodoc:
5
- # Sets the parent object
6
- def parentize(object, association_name)
7
- self.parent = object
8
- self.association_name = association_name
9
- add_observer(object)
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,19 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb"))
2
-
3
- describe Mongoid::Extensions::Object::Parentization do
4
-
5
- describe "#parentize" do
6
-
7
- before do
8
- @parent = Person.new
9
- @child = Name.new
10
- end
11
-
12
- it "sets the parent on each element" do
13
- @child.parentize(@parent, :child)
14
- @child.parent.should == @parent
15
- end
16
-
17
- end
18
-
19
- end