Class: Mongoid::Associations::HasMany
- Includes:
- Proxy
- Defined in:
- lib/mongoid/associations/has_many.rb
Instance Attribute Summary collapse
-
#association_name ⇒ Object
Returns the value of attribute association_name.
-
#klass ⇒ Object
Returns the value of attribute klass.
Class Method Summary collapse
-
.instantiate(document, options) ⇒ Object
Preferred method of creating a new
HasMany
association. -
.macro ⇒ Object
Returns the macro used to create the association.
-
.update(children, parent, options) ⇒ Object
Perform an update of the relationship of the parent and child.
Instance Method Summary collapse
-
#<<(*objects) ⇒ Object
Appends the object to the
Array
, setting its parent in the process. -
#build(attributes) ⇒ Object
Builds a new Document and adds it to the association collection.
-
#clear ⇒ Object
Clears the association, and notifies the parents of the removal.
-
#concat(*objects) ⇒ Object
Appends the object to the
Array
, setting its parent in the process. -
#create(attributes) ⇒ Object
Creates a new Document and adds it to the association collection.
-
#find(param) ⇒ Object
Finds a document in this association.
-
#initialize(document, options) ⇒ HasMany
constructor
Creates the new association by finding the attributes in the parent document with its name, and instantiating a new document for each one found.
-
#method_missing(name, *args, &block) ⇒ Object
Delegate all missing methods over to the documents array.
-
#nested_build(attributes) ⇒ Object
Used for setting associations via a nested attributes setter from the parent
Document
. -
#push(*objects) ⇒ Object
Appends the object to the
Array
, setting its parent in the process.
Methods included from Proxy
Constructor Details
#initialize(document, options) ⇒ HasMany
Creates the new association by finding the attributes in the parent document with its name, and instantiating a new document for each one found. These will then be put in an internal array.
This then delegated all methods to the array class since this is essentially a proxy to an array itself.
71 72 73 74 75 76 77 78 79 |
# File 'lib/mongoid/associations/has_many.rb', line 71 def initialize(document, ) @parent, @association_name, @klass = document, .name, .klass attributes = document.attributes[@association_name] @documents = attributes ? attributes.collect do |attribute| child = @klass.instantiate(attribute) child.parentize(@parent, @association_name) child end : [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Delegate all missing methods over to the documents array.
82 83 84 |
# File 'lib/mongoid/associations/has_many.rb', line 82 def method_missing(name, *args, &block) @documents.send(name, *args, &block) end |
Instance Attribute Details
#association_name ⇒ Object
Returns the value of attribute association_name.
7 8 9 |
# File 'lib/mongoid/associations/has_many.rb', line 7 def association_name @association_name end |
#klass ⇒ Object
Returns the value of attribute klass.
7 8 9 |
# File 'lib/mongoid/associations/has_many.rb', line 7 def klass @klass end |
Class Method Details
.instantiate(document, options) ⇒ Object
Preferred method of creating a new HasMany
association. It will delegate to new.
Options:
document: The parent Document
options: The association options
113 114 115 |
# File 'lib/mongoid/associations/has_many.rb', line 113 def instantiate(document, ) new(document, ) end |
.macro ⇒ Object
Returns the macro used to create the association.
118 119 120 |
# File 'lib/mongoid/associations/has_many.rb', line 118 def macro :has_many end |
.update(children, parent, options) ⇒ Object
Perform an update of the relationship of the parent and child. This is initialized by setting the has_many to the supplied Enumerable
and setting up the parentization.
125 126 127 128 129 |
# File 'lib/mongoid/associations/has_many.rb', line 125 def update(children, parent, ) parent.remove_attribute(.name) children.assimilate(parent, ) new(parent, ) end |
Instance Method Details
#<<(*objects) ⇒ Object
Appends the object to the Array
, setting its parent in the process.
11 12 13 14 15 16 17 |
# File 'lib/mongoid/associations/has_many.rb', line 11 def <<(*objects) objects.flatten.each do |object| object.parentize(@parent, @association_name) @documents << object object.notify end end |
#build(attributes) ⇒ Object
Builds a new Document and adds it to the association collection. The document created will be of the same class as the others in the association, and the attributes will be passed into the constructor.
Returns the newly created object.
38 39 40 41 42 43 |
# File 'lib/mongoid/associations/has_many.rb', line 38 def build(attributes) object = @klass.instantiate(attributes) object.parentize(@parent, @association_name) push(object) object end |
#clear ⇒ Object
Clears the association, and notifies the parents of the removal.
20 21 22 23 24 25 |
# File 'lib/mongoid/associations/has_many.rb', line 20 def clear object = @documents.first object.changed(true) object.notify_observers(object, true) @documents.clear end |
#concat(*objects) ⇒ Object
Appends the object to the Array
, setting its parent in the process.
29 30 31 |
# File 'lib/mongoid/associations/has_many.rb', line 29 def concat(*objects) self << objects end |
#create(attributes) ⇒ Object
Creates a new Document and adds it to the association collection. The document created will be of the same class as the others in the association, and the attributes will be passed into the constructor and the new object will then be saved.
Returns the newly created object.
51 52 53 54 |
# File 'lib/mongoid/associations/has_many.rb', line 51 def create(attributes) object = build(attributes) object.save end |
#find(param) ⇒ Object
Finds a document in this association. If :all is passed, returns all the documents If an id is passed, will return the document for that id.
59 60 61 62 |
# File 'lib/mongoid/associations/has_many.rb', line 59 def find(param) return @documents if param == :all return detect { |document| document.id == param } end |
#nested_build(attributes) ⇒ Object
Used for setting associations via a nested attributes setter from the parent Document
.
Options:
attributes: A Hash
of integer keys and Hash
values.
92 93 94 95 96 |
# File 'lib/mongoid/associations/has_many.rb', line 92 def nested_build(attributes) attributes.values.each do |attrs| build(attrs) end end |
#push(*objects) ⇒ Object
Appends the object to the Array
, setting its parent in the process.
100 101 102 |
# File 'lib/mongoid/associations/has_many.rb', line 100 def push(*objects) self << objects end |