Module: Mongoid::Fields::Serializable

Overview

Defines the behaviour for defined fields in the document.

For people who want to have custom field types in their applications and want control over the serialization process to and from the domain model and MongoDB you will need to include this module in your custom type class. You will also need to define either a #serialize and #deserialize instance method, where previously these were a .set and .get class method respectively.

class MyCustomType
  include Mongoid::Fields::Serializable

  def deserialize(object)
    # Do something to convert it from Mongo to my type.
  end

  def serialize(object)
    # Do something to convert from my type to MongoDB friendly.
  end
end

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_valObject

Set readers for the instance variables.



38
39
40
# File 'lib/mongoid/fields/serializable.rb', line 38

def default_val
  @default_val
end

#labelObject

Set readers for the instance variables.



38
39
40
# File 'lib/mongoid/fields/serializable.rb', line 38

def label
  @label
end

#localizeObject

Set readers for the instance variables.



38
39
40
# File 'lib/mongoid/fields/serializable.rb', line 38

def localize
  @localize
end

#nameObject

Set readers for the instance variables.



38
39
40
# File 'lib/mongoid/fields/serializable.rb', line 38

def name
  @name
end

#optionsObject

Set readers for the instance variables.



38
39
40
# File 'lib/mongoid/fields/serializable.rb', line 38

def options
  @options
end

Instance Method Details

#constraintConstraint

Get the constraint from the metadata once.

Examples:

Get the constraint.

field.constraint

Returns:

  • (Constraint)

    The relation’s contraint.

Since:

  • 2.1.0



48
49
50
# File 'lib/mongoid/fields/serializable.rb', line 48

def constraint
  @constraint ||= .constraint
end

#deserialize(object) ⇒ Object

Deserialize this field from the type stored in MongoDB to the type defined on the model

Examples:

Deserialize the field.

field.deserialize(object)

Parameters:

  • object (Object)

    The object to cast.

Returns:

  • (Object)

    The converted object.

Since:

  • 2.1.0



63
# File 'lib/mongoid/fields/serializable.rb', line 63

def deserialize(object); object; end

#eval_default(doc) ⇒ Object

Evaluate the default value and return it. Will handle the serialization, proc calls, and duplication if necessary.

Examples:

Evaluate the default value.

field.eval_default(document)

Parameters:

  • doc (Document)

    The document the field belongs to.

Returns:

  • (Object)

    The serialized default value.

Since:

  • 2.1.8



76
77
78
79
80
81
82
# File 'lib/mongoid/fields/serializable.rb', line 76

def eval_default(doc)
  if fields = Threaded.selection
    evaluated_default(doc) if included?(fields)
  else
    evaluated_default(doc)
  end
end

#foreign_key?true, false

Is this field a foreign key?

Examples:

Is the field a foreign key?

field.foreign_key?

Returns:

  • (true, false)

    If the field is a foreign key.

Since:

  • 2.4.0



92
93
94
# File 'lib/mongoid/fields/serializable.rb', line 92

def foreign_key?
  !!options[:identity]
end

#localized?true, false

Is the field localized or not?

Examples:

Is the field localized?

field.localized?

Returns:

  • (true, false)

    If the field is localized.

Since:

  • 2.3.0



104
105
106
# File 'lib/mongoid/fields/serializable.rb', line 104

def localized?
  !!@localize
end

#metadataMetadata

Get the metadata for the field if its a foreign key.

Examples:

Get the metadata.

field.

Returns:

  • (Metadata)

    The relation metadata.

Since:

  • 2.2.0



116
117
118
# File 'lib/mongoid/fields/serializable.rb', line 116

def 
  @metadata ||= options[:metadata]
end

#object_id_field?true, false

Is the field a BSON::ObjectId?

Examples:

Is the field a BSON::ObjectId?

field.object_id_field?

Returns:

  • (true, false)

    If the field is a BSON::ObjectId.

Since:

  • 2.2.0



128
129
130
# File 'lib/mongoid/fields/serializable.rb', line 128

def object_id_field?
  @object_id_field ||= (type == BSON::ObjectId)
end

#resizable?false

Can the field vary in size, similar to arrays.

Examples:

Is the field varying in size?

field.resizable?

Returns:

  • (false)

    false by default.

Since:

  • 2.4.0



140
# File 'lib/mongoid/fields/serializable.rb', line 140

def resizable?; false; end

#selection(object) ⇒ Object

Convert the provided object to a Mongoid criteria friendly value.

Examples:

Convert the field.

field.selection(object)

Parameters:

  • The (Object)

    object to convert.

Returns:

  • (Object)

    The converted object.

Since:

  • 2.4.0



165
# File 'lib/mongoid/fields/serializable.rb', line 165

def selection(object); object; end

#serialize(object) ⇒ Object

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:

  • (Object)

    The converted object.

Since:

  • 2.1.0



153
# File 'lib/mongoid/fields/serializable.rb', line 153

def serialize(object); object; end

#typeClass

Get the type of this field - inferred from the class name.

Examples:

Get the type.

field.type

Returns:

  • (Class)

    The name of the class.

Since:

  • 2.1.0



175
176
177
# File 'lib/mongoid/fields/serializable.rb', line 175

def type
  @type ||= options[:type] || Object
end

#versioned?true, false

Is this field included in versioned attributes?

Examples:

Is the field versioned?

field.versioned?

Returns:

  • (true, false)

    If the field is included in versioning.

Since:

  • 2.1.0



187
188
189
# File 'lib/mongoid/fields/serializable.rb', line 187

def versioned?
  @versioned ||= (options[:versioned].nil? ? true : options[:versioned])
end