123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Extensions::Hash

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/mongoid/extensions/hash.rb

Overview

Adds type-casting behavior to Hash class.

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#resizable?true (readonly)

Can the size of this object change?

Examples:

Is the hash resizable?

{}.resizable?

Returns:

  • (true)

    true.

[ GitHub ]

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

def resizable?
  true
end

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.

[ GitHub ]

  
# 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.

[ GitHub ]

  
# 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.

[ GitHub ]

  
# 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.

[ GitHub ]

  
# 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.

[ GitHub ]

  
# 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.

[ GitHub ]

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

def mongoize
  ::Hash.mongoize(self)
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:

[ GitHub ]

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

def to_criteria
  Criteria.from_hash(self)
end