Class: Mongoid::Fields::Internal::Array

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

Overview

Defines the behaviour for array 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?, #selection, #type, #versioned?

Instance Method Details

#add_atomic_changes(document, key, mods, new, old) ⇒ Object

Adds the atomic changes for this type of resizable field.

Examples:

Add the atomic changes.

field.add_atomic_changes(doc, "key", {}, [], [])

Parameters:

  • document (Document)

    The document to add to.

  • key (String)

    The name of the field.

  • mods (Hash)

    The current modifications.

  • new (Array)

    The new elements to add.

  • old (Array)

    The old elements getting removed.

Since:

  • 2.4.0



22
23
24
25
26
27
28
29
30
# File 'lib/mongoid/fields/internal/array.rb', line 22

def add_atomic_changes(document, key, mods, new, old)
  if new.any? && old.any?
    mods[key] = document.attributes[key]
  elsif new.any?
    document.atomic_array_pushes[key] = new
  elsif old.any?
    document.atomic_array_pulls[key] = old
  end
end

#resizable?true

Array fields are resizable.

Examples:

Is this field resizable?

field.resizable?

Returns:

  • (true)

    Always true.

Since:

  • 2.4.0



40
# File 'lib/mongoid/fields/internal/array.rb', line 40

def resizable?; true; end

#serialize(object) ⇒ Array

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:

  • (Array)

    The converted object.

Since:

  • 2.1.0



53
54
55
# File 'lib/mongoid/fields/internal/array.rb', line 53

def serialize(object)
  raise_or_return(object)
end