module Mongoid::Extensions::Hash

Adds type-casting behavior to Hash class.

Public Instance Methods

__consolidate__(klass) click to toggle source

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.

@example Consolidate the hash.

{ name: "Placebo" }.__consolidate__

@return [ Hash ] A new consolidated hash.

@deprecated

# File lib/mongoid/extensions/hash.rb, line 45
def __consolidate__(klass)
  Mongoid::AtomicUpdatePreparer.prepare(self, klass)
end
__evolve_object_id__() click to toggle source

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

@example Convert the hash values.

{ field: id }.__evolve_object_id__

@return [ Hash ] The converted hash.

# File lib/mongoid/extensions/hash.rb, line 16
def __evolve_object_id__
  transform_values!(&:__evolve_object_id__)
end
__mongoize_object_id__() click to toggle source

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

@example Convert the hash values.

{ field: id }.__mongoize_object_id__

@return [ Hash ] The converted hash.

# 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_id() click to toggle source

Deletes an id value from the hash.

@example Delete an id value.

{}.delete_id

@return [ Object ] The deleted value, or nil. @deprecated

# File lib/mongoid/extensions/hash.rb, line 57
def delete_id
  delete("_id") || delete(:_id) || delete("id") || delete(:id)
end
extract_id() click to toggle source

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

@example Extract the id.

{ :_id => 1 }.extract_id

@return [ Object ] The value of the id. @deprecated

# File lib/mongoid/extensions/hash.rb, line 70
def extract_id
  self["_id"] || self[:_id] || self["id"] || self[:id]
end
mongoize() click to toggle source

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

@example Mongoize the object.

object.mongoize

@return [ Hash | nil ] The object mongoized or nil.

# File lib/mongoid/extensions/hash.rb, line 82
def mongoize
  ::Hash.mongoize(self)
end
resizable?() click to toggle source

Can the size of this object change?

@example Is the hash resizable?

{}.resizable?

@return [ true ] true.

# File lib/mongoid/extensions/hash.rb, line 92
def resizable?
  true
end
to_criteria() click to toggle source

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.

@example Convert the hash to a criteria.

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

@return [ Criteria ] The criteria. @deprecated

# File lib/mongoid/extensions/hash.rb, line 105
def to_criteria
  Criteria.from_hash(self)
end