123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Factory::Instantiator Private

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: lib/mongoid/factory.rb

Overview

A helper class for instantiating a model using either it’s type class directly, or via a type class specified via a discriminator key.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#attributesHash | nil (readonly)

Returns:

  • (Hash | nil)

    The Hash of attributes to use when instantiating the model.

[ GitHub ]

  
# File 'lib/mongoid/factory.rb', line 19

attr_reader :attributes

#criteriaMongoid::Criteria | nil (readonly)

Returns:

  • (Mongoid::Criteria | nil)

    The criteria object to use as a secondary source for the selected fields; also used when setting the inverse association.

[ GitHub ]

  
# File 'lib/mongoid/factory.rb', line 24

attr_reader :criteria

#klassMongoid::Document (readonly)

Returns:

[ GitHub ]

  
# File 'lib/mongoid/factory.rb', line 15

attr_reader :klass

#selected_fieldsArray | nil (readonly)

Returns:

  • (Array | nil)

    The list of field names that should be explicitly (and exclusively) included in the new record.

[ GitHub ]

  
# File 'lib/mongoid/factory.rb', line 28

attr_reader :selected_fields

#typeString | nil (readonly)

Returns:

  • (String | nil)

    The identifier of the class that should be loaded and instantiated, in the case of a polymorphic class specification.

[ GitHub ]

  
# 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.

Parameters:

  • type (String)

    The name of the type to constantize

Returns:

  • (Class)

    the Class that the type resolves to

Raises:

[ GitHub ]

  
# File 'lib/mongoid/factory.rb', line 127

def constantize(type)
  camelized = type.camelize
  camelized.constantize
rescue NameError
  raise Errors::UnknownModel.new(camelized, type)
end

#constantized_typeMongoid::Document (private)

Retreive 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.

Returns:

[ GitHub ]

  
# 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.

Parameters:

  • execute_callbacks (true | false)

    Whether or not the ::Mongoid::Document callbacks should be invoked with the new instance.

Returns:

Raises:

  • (Errors::UnknownModel)

    when the requested type does not exist, or if it does not respond to the instantiate method.

[ GitHub ]

  
# 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.

Parameters:

  • execute_callbacks (true | false)

    Whether this method should invoke document callbacks.

Returns:

  • (Document)

    The instantiated document.

[ GitHub ]

  
# 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.

Parameters:

  • execute_callbacks (true | false)

    Whether this method should invoke document callbacks.

Returns:

  • (Document)

    The instantiated document.

[ GitHub ]

  
# 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