Class: Mongoid::Fields::Internal::ObjectId

Inherits:
Object
  • Object
show all
Includes:
Serializable
Defined in:
lib/mongoid/fields/internal/object_id.rb

Overview

Defines the behaviour for BSON::ObjectId fields.

Instance Attribute Summary

Attributes included from Serializable

#default_val, #label, #localize, #name, #options

Instance Method Summary collapse

Methods included from Serializable

#constraint, #deserialize, #eval_default, #foreign_key?, #localized?, #metadata, #object_id_field?, #resizable?, #type, #versioned?

Instance Method Details

#selection(object) ⇒ Object

Special case to serialize the object.

Examples:

Convert to a selection.

field.selection(object)

Parameters:

  • The (Object)

    object to convert.

Returns:

  • (Object)

    The converted object.

Since:

  • 2.4.0



20
21
22
23
# File 'lib/mongoid/fields/internal/object_id.rb', line 20

def selection(object)
  return object if object.is_a?(::Hash)
  serialize(object)
end

#serialize(object) ⇒ BSON::ObjectId

Serialize the object from the type defined in the model to a MongoDB compatible object to store.

Examples:

Serialize the field.

field.serialize(object)

Parameters:

  • object (Object)

    The object to cast.

Returns:

Since:

  • 2.1.0



36
37
38
39
40
41
42
# File 'lib/mongoid/fields/internal/object_id.rb', line 36

def serialize(object)
  if object.is_a?(::String)
    BSON::ObjectId.from_string(object) unless object.blank?
  else
    object
  end
end