mongoid 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -56,7 +56,7 @@ Example of a simple domain model:
56
56
  end
57
57
  </pre>
58
58
 
59
- Mongoid suuports all the basic ActiveRecord creation and persistence methods.
59
+ Mongoid supports all the basic ActiveRecord creation and persistence methods.
60
60
 
61
61
  Creation:
62
62
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.4
1
+ 0.5.5
@@ -47,8 +47,8 @@ module Mongoid # :nodoc:
47
47
  # class Address < Mongoid::Document
48
48
  # belongs_to :person
49
49
  # end
50
- def has_many(association_name)
51
- add_association(:has_many, association_name.to_s.classify, association_name)
50
+ def has_many(association_name, options = {})
51
+ add_association(:has_many, association_name.to_s.classify, association_name, options)
52
52
  end
53
53
 
54
54
  # Adds the association from a parent document to its child. The name
@@ -68,16 +68,16 @@ module Mongoid # :nodoc:
68
68
  # class Address < Mongoid::Document
69
69
  # belongs_to :person
70
70
  # end
71
- def has_one(association_name)
72
- add_association(:has_one, association_name.to_s.titleize, association_name)
71
+ def has_one(association_name, options = {})
72
+ add_association(:has_one, association_name.to_s.titleize, association_name, options)
73
73
  end
74
74
 
75
75
  private
76
76
  # Adds the association to the associations hash with the type as the key,
77
77
  # then adds the accessors for the association.
78
- def add_association(type, class_name, name)
78
+ def add_association(type, class_name, name, options = {})
79
79
  define_method(name) do
80
- Associations::Factory.create(type, name, self)
80
+ Associations::Factory.create(type, name, self, options)
81
81
  end
82
82
  define_method("#{name}=") do |object|
83
83
  object.parentize(self, name)
@@ -21,6 +21,10 @@ module Mongoid #:nodoc:
21
21
  @document
22
22
  end
23
23
 
24
+ # Delegate equality to the parent document.
25
+ def ==(other)
26
+ @document == other
27
+ end
24
28
  end
25
29
  end
26
30
  end
@@ -7,11 +7,11 @@ module Mongoid #:nodoc:
7
7
  # association.
8
8
  #
9
9
  # If the type is invalid a InvalidAssociationError will be thrown.
10
- def self.create(association_type, association_name, document)
10
+ def self.create(association_type, association_name, document, options = {})
11
11
  case association_type
12
12
  when :belongs_to then BelongsToAssociation.new(document)
13
- when :has_many then HasManyAssociation.new(association_name, document)
14
- when :has_one then HasOneAssociation.new(association_name, document)
13
+ when :has_many then HasManyAssociation.new(association_name, document, options)
14
+ when :has_one then HasOneAssociation.new(association_name, document, options)
15
15
  else raise InvalidAssociationError
16
16
  end
17
17
  end
@@ -51,10 +51,11 @@ module Mongoid #:nodoc:
51
51
  #
52
52
  # This then delegated all methods to the array class since this is
53
53
  # essentially a proxy to an array itself.
54
- def initialize(association_name, document)
54
+ def initialize(name, document, options = {})
55
55
  @parent = document
56
- @association_name = association_name
57
- @klass = @association_name.to_s.classify.constantize
56
+ @association_name = name
57
+ class_name = options[:class_name]
58
+ @klass = class_name ? class_name.constantize : @association_name.to_s.classify.constantize
58
59
  attributes = document.attributes[@association_name]
59
60
  @documents = attributes ? attributes.collect do |attribute|
60
61
  child = @klass.new(attribute)
@@ -11,14 +11,19 @@ module Mongoid #:nodoc:
11
11
  #
12
12
  # All method calls on this object will then be delegated
13
13
  # to the internal document itself.
14
- def initialize(association_name, document)
15
- klass = association_name.to_s.titleize.constantize
16
- attributes = document.attributes[association_name]
14
+ def initialize(name, document, options = {})
15
+ class_name = options[:class_name]
16
+ klass = class_name ? class_name.constantize : name.to_s.titleize.constantize
17
+ attributes = document.attributes[name]
17
18
  @document = klass.new(attributes)
18
19
  @document.parent = document
19
20
  decorate!
20
21
  end
21
22
 
23
+ # Equality delegates to the document.
24
+ def ==(other)
25
+ @document == other
26
+ end
22
27
  end
23
28
  end
24
29
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.5.4"
8
+ s.version = "0.5.5"
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-10-28}
12
+ s.date = %q{2009-10-30}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -21,7 +21,9 @@ class Person < Mongoid::Document
21
21
  field :terms, :type => Boolean
22
22
  field :age, :type => Integer
23
23
  has_many :addresses
24
+ has_many :phone_numbers, :class_name => "Phone"
24
25
  has_one :name
26
+ has_one :pet, :class_name => "Animal"
25
27
  end
26
28
 
27
29
  class Address < Mongoid::Document
@@ -33,6 +35,25 @@ class Address < Mongoid::Document
33
35
  belongs_to :person
34
36
  end
35
37
 
38
+ class Phone < Mongoid::Document
39
+ field :number
40
+ key :number
41
+ belongs_to :person
42
+ has_one :country_code
43
+ end
44
+
45
+ class CountryCode < Mongoid::Document
46
+ field :code, :type => Integer
47
+ key :code
48
+ belongs_to :phone_number
49
+ end
50
+
51
+ class Animal < Mongoid::Document
52
+ field :name
53
+ key :name
54
+ belongs_to :person
55
+ end
56
+
36
57
  class Name < Mongoid::Document
37
58
  field :first_name
38
59
  field :last_name
@@ -49,6 +49,13 @@ describe Mongoid::Associations do
49
49
  address.should respond_to(:person=)
50
50
  end
51
51
 
52
+ it "allows the parent to be any type of class" do
53
+ phone_number = Phone.new(:number => "415-555-1212")
54
+ code = CountryCode.new(:code => 1)
55
+ phone_number.country_code = code
56
+ code.phone_number.should == phone_number
57
+ end
58
+
52
59
  end
53
60
 
54
61
  describe "#has_many" do
@@ -86,6 +93,21 @@ describe Mongoid::Associations do
86
93
 
87
94
  end
88
95
 
96
+ context "when a class_name is supplied" do
97
+
98
+ before do
99
+ @attributes = { :title => "Sir",
100
+ :phone_numbers => [ { :number => "404-555-1212" } ]
101
+ }
102
+ @person = Person.new(@attributes)
103
+ end
104
+
105
+ it "sets the association name" do
106
+ @person.phone_numbers.first.should == Phone.new(:number => "404-555-1212")
107
+ end
108
+
109
+ end
110
+
89
111
  end
90
112
 
91
113
  describe "#has_one" do
@@ -121,6 +143,21 @@ describe Mongoid::Associations do
121
143
 
122
144
  end
123
145
 
146
+ context "when a class_name is supplied" do
147
+
148
+ before do
149
+ @attributes = { :title => "Sir",
150
+ :pet => { :name => "Fido" }
151
+ }
152
+ @person = Person.new(@attributes)
153
+ end
154
+
155
+ it "sets the association name" do
156
+ @person.pet.should == Animal.new(:name => "Fido")
157
+ end
158
+
159
+ end
160
+
124
161
  end
125
162
 
126
163
  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.5.4
4
+ version: 0.5.5
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-10-28 00:00:00 -04:00
12
+ date: 2009-10-30 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency