Class: Mongoid::Factory::Instantiator Private
- Inherits:
-
Object
- Object
- Mongoid::Factory::Instantiator
- Defined in:
- lib/mongoid/factory.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A helper class for instantiating a model using either it’s type class directly, or via a type class specified via a discriminator key.
Instance Attribute Summary collapse
-
#attributes ⇒ Hash | nil
readonly
private
The Hash of attributes to use when instantiating the model.
-
#criteria ⇒ Mongoid::Criteria | nil
readonly
private
The criteria object to use as a secondary source for the selected fields; also used when setting the inverse association.
-
#klass ⇒ Mongoid::Document
readonly
private
The primary model class being referenced.
-
#selected_fields ⇒ Array | nil
readonly
private
The list of field names that should be explicitly (and exclusively) included in the new record.
-
#type ⇒ String | nil
readonly
private
The identifier of the class that should be loaded and instantiated, in the case of a polymorphic class specification.
Instance Method Summary collapse
-
#initialize(klass, attributes, criteria, selected_fields) ⇒ Instantiator
constructor
private
Creates a new Factory::Initiator.
-
#instance(execute_callbacks: Threaded.execute_callbacks?) ⇒ Mongoid::Document
private
Builds and returns a new instance of the requested class.
Constructor Details
#initialize(klass, attributes, criteria, selected_fields) ⇒ Instantiator
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.
Creates a new Factory::Initiator.
46 47 48 49 50 51 52 53 |
# File 'lib/mongoid/factory.rb', line 46 def initialize(klass, attributes, criteria, selected_fields) @klass = klass @attributes = attributes @criteria = criteria @selected_fields = selected_fields || (criteria && criteria.[:fields]) @type = attributes && attributes[klass.discriminator_key] end |
Instance Attribute Details
#attributes ⇒ Hash | nil (readonly)
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.
Returns The Hash of attributes to use when instantiating the model.
19 20 21 |
# File 'lib/mongoid/factory.rb', line 19 def attributes @attributes end |
#criteria ⇒ Mongoid::Criteria | nil (readonly)
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.
Returns The criteria object to use as a secondary source for the selected fields; also used when setting the inverse association.
24 25 26 |
# File 'lib/mongoid/factory.rb', line 24 def criteria @criteria end |
#klass ⇒ Mongoid::Document (readonly)
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.
Returns The primary model class being referenced.
15 16 17 |
# File 'lib/mongoid/factory.rb', line 15 def klass @klass end |
#selected_fields ⇒ Array | nil (readonly)
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.
Returns The list of field names that should be explicitly (and exclusively) included in the new record.
28 29 30 |
# File 'lib/mongoid/factory.rb', line 28 def selected_fields @selected_fields end |
#type ⇒ String | nil (readonly)
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.
Returns The identifier of the class that should be loaded and instantiated, in the case of a polymorphic class specification.
33 34 35 |
# File 'lib/mongoid/factory.rb', line 33 def type @type end |
Instance Method Details
#instance(execute_callbacks: Threaded.execute_callbacks?) ⇒ Mongoid::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.
Builds and returns a new instance of the requested class.
64 65 66 67 68 69 70 |
# File 'lib/mongoid/factory.rb', line 64 def instance(execute_callbacks: Threaded.execute_callbacks?) if type.blank? instantiate_without_type(execute_callbacks) else instantiate_with_type(execute_callbacks) end end |