Module: Mongoid::Extensions::Hash

Defined in:
lib/mongoid/extensions/hash.rb

Overview

Adds type-casting behavior to Hash class.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#__consolidate__(klass) ⇒ Hash

Deprecated.

Consolidate the key/values in the hash under an atomic $set. DEPRECATED. This was never intended to be a public API and the functionality will no longer be exposed once this method is eventually removed.

Examples:

Consolidate the hash.

{ name: "Placebo" }.__consolidate__

Returns:

  • (Hash)

    A new consolidated hash.



45
46
47
# File 'lib/mongoid/extensions/hash.rb', line 45

def __consolidate__(klass)
  Mongoid::AtomicUpdatePreparer.prepare(self, klass)
end

#__evolve_object_id__Hash

Evolves each value in the hash to an object id if it is convertable.

Examples:

Convert the hash values.

{ field: id }.__evolve_object_id__

Returns:

  • (Hash)

    The converted hash.



16
17
18
# File 'lib/mongoid/extensions/hash.rb', line 16

def __evolve_object_id__
  transform_values!(&:__evolve_object_id__)
end

#__mongoize_object_id__Hash

Mongoizes each value in the hash to an object id if it is convertable.

Examples:

Convert the hash values.

{ field: id }.__mongoize_object_id__

Returns:

  • (Hash)

    The converted hash.



26
27
28
29
30
31
32
# File 'lib/mongoid/extensions/hash.rb', line 26

def __mongoize_object_id__
  if id = self['$oid']
    BSON::ObjectId.from_string(id)
  else
    transform_values!(&:__mongoize_object_id__)
  end
end

#delete_idObject

Deprecated.

Deletes an id value from the hash.

Examples:

Delete an id value.

{}.delete_id

Returns:

  • (Object)

    The deleted value, or nil.



57
58
59
# File 'lib/mongoid/extensions/hash.rb', line 57

def delete_id
  delete("_id") || delete(:_id) || delete("id") || delete(:id)
end

#extract_idObject

Deprecated.

Get the id attribute from this hash, whether it’s prefixed with an underscore or is a symbol.

Examples:

Extract the id.

{ :_id => 1 }.extract_id

Returns:

  • (Object)

    The value of the id.



70
71
72
# File 'lib/mongoid/extensions/hash.rb', line 70

def extract_id
  self["_id"] || self[:_id] || self["id"] || self[:id]
end

#mongoizeHash | nil

Turn the object from the ruby type we deal with to a Mongo friendly type.

Examples:

Mongoize the object.

object.mongoize

Returns:

  • (Hash | nil)

    The object mongoized or nil.



82
83
84
# File 'lib/mongoid/extensions/hash.rb', line 82

def mongoize
  ::Hash.mongoize(self)
end

#resizable?true

Can the size of this object change?

Examples:

Is the hash resizable?

{}.resizable?

Returns:

  • (true)

    true.



92
93
94
# File 'lib/mongoid/extensions/hash.rb', line 92

def resizable?
  true
end

#to_criteriaCriteria

Deprecated.

Convert this hash to a criteria. Will iterate over each keys in the hash which must correspond to method on a criteria object. The hash must also include a “klass” key.

Examples:

Convert the hash to a criteria.

{ klass: Band, where: { name: "Depeche Mode" }.to_criteria

Returns:



105
106
107
# File 'lib/mongoid/extensions/hash.rb', line 105

def to_criteria
  Criteria.from_hash(self)
end