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

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

Overview

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

Instance Method Details

#add_atomic_changes(document, name, key, mods, new_elements, old_elements) ⇒ Object

Adds the atomic changes for this type of resizable field.

@todo: Durran: Refactor, big time.

Examples:

Add the atomic changes.

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

Parameters:

  • document (Document)

    The document to add to.

  • name (String)

    The name of the field.

  • key (String)

    The atomic location 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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 26

def add_atomic_changes(document, name, key, mods, new_elements, old_elements)
  old = (old_elements || [])
  new = (new_elements || [])
  if new.length > old.length
    if new.first(old.length) == old
      document.atomic_array_add_to_sets[key] = new.drop(old.length)
    else
      mods[key] = document.attributes[name]
    end
  elsif new.length < old.length
    pulls = old - new
    if new == old - pulls
      document.atomic_array_pulls[key] = pulls
    else
      mods[key] = document.attributes[name]
    end
  elsif new != old
    mods[key] = document.attributes[name]
  end
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



55
56
57
58
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 55

def object_id_field?
  @object_id_field ||=
    .polymorphic? ? true : .klass.using_object_ids?
end

#resizable?true

Array fields are resizable.

Examples:

Is this field resizable?

field.resizable?

Returns:

  • (true)

    Always true.

Since:

  • 2.4.0



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

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



81
82
83
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 81

def serialize(object)
  object ? constraint.convert(object) : []
end