Class: Mongoid::Fields::Serializable::ObjectId

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

Overview

Defines the behaviour for BSON::ObjectId fields.

Instance Attribute Summary

Attributes included from Mongoid::Fields::Serializable

#default, #label, #name, #options

Instance Method Summary collapse

Methods included from Mongoid::Fields::Serializable

#cast_on_read?, #constraint, #deserialize, #eval_default, #metadata, #object_id_field?, #type, #versioned?

Instance Method Details

#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



21
22
23
24
25
26
27
# File 'lib/mongoid/fields/serializable/object_id.rb', line 21

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