Class: Mongoid::Factory::Instantiator Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • klass (Mongoid::Document)

    The primary class to reference when instantiating the model.

  • attributes (Hash | nil)

    (Optional) The hash of attributes to use when instantiating the model.

  • criteria (Mongoid::Criteria | nil)

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

  • selected_fields (Array | nil)

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



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.options[:fields])
  @type = attributes && attributes[klass.discriminator_key]
end

Instance Attribute Details

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

Returns:

  • (Hash | nil)

    The Hash of attributes to use when instantiating the model.



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

def attributes
  @attributes
end

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

Returns:

  • (Mongoid::Criteria | nil)

    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

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

Returns:



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

def klass
  @klass
end

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

Returns:

  • (Array | nil)

    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

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

Returns:

  • (String | nil)

    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.

Parameters:

  • execute_callbacks (true | false) (defaults to: Threaded.execute_callbacks?)

    Whether or not the 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.



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