Module: Mongoid::Factory
Overview
Instantiates documents that came from the database.
Defined Under Namespace
Classes: Instantiator
Instance Method Summary collapse
-
#build(klass, attributes = nil) ⇒ Document
Builds a new
Document
from the supplied attributes. -
#execute_build(klass, attributes = nil, options = {}) ⇒ Document
private
Execute the build.
-
#execute_from_db(klass, attributes = nil, criteria = nil, selected_fields = nil, execute_callbacks: Threaded.execute_callbacks?) ⇒ Document
private
Execute from_db.
-
#from_db(klass, attributes = nil, criteria = nil, selected_fields = nil) ⇒ Document
Builds a new
Document
from the supplied attributes loaded from the database.
Instance Method Details
#build(klass, attributes = nil) ⇒ Document
Builds a new Document
from the supplied attributes.
This method either instantiates klass or a descendant of klass if the attributes include klass’ discriminator key.
If the attributes contain the discriminator key (which is _type by default) and the discriminator value does not correspond to a descendant of klass then this method would create an instance of klass.
154 155 156 |
# File 'lib/mongoid/factory.rb', line 154 def build(klass, attributes = nil) execute_build(klass, attributes) end |
#execute_build(klass, attributes = nil, options = {}) ⇒ Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A Ruby 2.x bug prevents the options hash from being keyword arguments. Once we drop support for Ruby 2.x, we can reimplement the options hash as keyword arguments. See bugs.ruby-lang.org/issues/15753
Execute the build.
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/mongoid/factory.rb', line 175 def execute_build(klass, attributes = nil, = {}) attributes ||= {} dvalue = attributes[klass.discriminator_key] || attributes[klass.discriminator_key.to_sym] type = klass.get_discriminator_mapping(dvalue) if type type.construct_document(attributes, ) else klass.construct_document(attributes, ) end end |
#execute_from_db(klass, attributes = nil, criteria = nil, selected_fields = nil, execute_callbacks: Threaded.execute_callbacks?) ⇒ Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Execute from_db.
234 235 236 237 238 239 |
# File 'lib/mongoid/factory.rb', line 234 def execute_from_db(klass, attributes = nil, criteria = nil, selected_fields = nil, execute_callbacks: Threaded.execute_callbacks?) Instantiator.new(klass, attributes, criteria, selected_fields) .instance(execute_callbacks: execute_callbacks) end |
#from_db(klass, attributes = nil, criteria = nil, selected_fields = nil) ⇒ Document
Builds a new Document
from the supplied attributes loaded from the database.
If the attributes contain the discriminator key (which is _type by default) and the discriminator value does not correspond to a descendant of klass then this method raises an UnknownModel error.
If a criteria object is given, it is used in two ways:
-
If the criteria has a list of fields specified via #only, only those fields are populated in the returned document.
-
If the criteria has a referencing association (i.e., this document is being instantiated as an association of another document), the other document is also populated in the returned document’s reverse association, if one exists.
212 213 214 |
# File 'lib/mongoid/factory.rb', line 212 def from_db(klass, attributes = nil, criteria = nil, selected_fields = nil) execute_from_db(klass, attributes, criteria, selected_fields) end |