Class: Mongoid::Factory::Instantiator Private
| Relationships & Source Files | |
| Inherits: | Object | 
| Defined in: | lib/mongoid/factory.rb | 
Overview
A helper class for instantiating a model using either its type class directly, or via a type class specified via a discriminator key.
Class Method Summary
- 
    
      .new(klass, attributes, criteria, selected_fields)  ⇒ Instantiator 
    
    constructor
    Internal use only
    Creates a new Factory::Initiator. 
Instance Attribute Summary
- #attributes ⇒ Hash | nil readonly Internal use only
- #criteria ⇒ Mongoid::Criteria | nil readonly Internal use only
- #klass ⇒ Mongoid::Document readonly Internal use only
- #selected_fields ⇒ Array | nil readonly Internal use only
- #type ⇒ String | nil readonly Internal use only
Instance Method Summary
- 
    
      #instance(execute_callbacks: Threaded.execute_callbacks?)  ⇒ Mongoid::Document 
    
    Internal use only
    Builds and returns a new instance of the requested class. 
- 
    
      #constantize(type)  ⇒ Class 
    
    private
    Internal use only
    Attempts to convert the argument into a Class object by camelizing it and treating the result as the name of a constant. 
- 
    
      #constantized_type  ⇒ Mongoid::Document 
    
    private
    Internal use only
    Retrieve the Classinstance of the requested type, either by finding it in the #klass discriminator mapping, or by otherwise finding a::Mongoid::Documentmodel with the given name.
- 
    
      #instantiate_with_type(execute_callbacks)  ⇒ Document 
    
    private
    Internal use only
    Instantiate the given #type, which must map to another ::Mongoid::Documentmodel.
- 
    
      #instantiate_without_type(execute_callbacks)  ⇒ Document 
    
    private
    Internal use only
    Instantiate the given class without any given subclass. 
Instance Attribute Details
    #attributes  ⇒ Hash | nil  (readonly)
  
# File 'lib/mongoid/factory.rb', line 19
attr_reader :attributes
    #criteria  ⇒ Mongoid::Criteria | nil  (readonly)
  
# File 'lib/mongoid/factory.rb', line 24
attr_reader :criteria
#klass ⇒ Mongoid::Document (readonly)
# File 'lib/mongoid/factory.rb', line 15
attr_reader :klass
    #selected_fields  ⇒ Array | nil  (readonly)
  
# File 'lib/mongoid/factory.rb', line 28
attr_reader :selected_fields
    #type  ⇒ String | nil  (readonly)
  
# File 'lib/mongoid/factory.rb', line 33
attr_reader :type
Instance Method Details
    #constantize(type)  ⇒ Class  (private)
  
Attempts to convert the argument into a Class object by camelizing it and treating the result as the name of a constant.
#constantized_type ⇒ Mongoid::Document (private)
Retrieve the Class instance of the requested type, either by finding it in the #klass discriminator mapping, or by otherwise finding a ::Mongoid::Document model with the given name.
# File 'lib/mongoid/factory.rb', line 107
def constantized_type @constantized_type ||= begin constantized = klass.get_discriminator_mapping(type) || constantize(type) # Check if the class is a Document class raise Errors::UnknownModel.new(camelized, type) unless constantized.respond_to?(:instantiate) constantized end end
#instance(execute_callbacks: Threaded.execute_callbacks?) ⇒ Mongoid::Document
Builds and returns a new instance of the requested class.
# 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
#instantiate_with_type(execute_callbacks) ⇒ Document (private)
Instantiate the given #type, which must map to another ::Mongoid::Document model.
# File 'lib/mongoid/factory.rb', line 95
def instantiate_with_type(execute_callbacks) constantized_type.instantiate_document( attributes, selected_fields, execute_callbacks: execute_callbacks ) end
#instantiate_without_type(execute_callbacks) ⇒ Document (private)
Instantiate the given class without any given subclass.
# File 'lib/mongoid/factory.rb', line 80
def instantiate_without_type(execute_callbacks) klass.instantiate_document(attributes, selected_fields, execute_callbacks: execute_callbacks).tap do |obj| if criteria&.association && criteria&.parent_document obj.set_relation(criteria.association.inverse, criteria.parent_document) end end end