Module: Mongoid::Extensions::Hash

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#__consolidate__Hash

Consolidate the key/values in the hash under an atomic $set.

Examples:

Consolidate the hash.

{ name: "Placebo" }.__consolidate__

Returns:

  • (Hash)

    A new consolidated hash.

Since:

  • 3.0.0



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mongoid/extensions/hash.rb', line 38

def __consolidate__
  consolidated = {}
  each_pair do |key, value|
    if key =~ /\$/
      (consolidated[key] ||= {}).merge!(value)
    else
      (consolidated["$set"] ||= {}).merge!(key => value)
    end
  end
  consolidated
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.

Since:

  • 3.0.0



14
15
16
# File 'lib/mongoid/extensions/hash.rb', line 14

def __evolve_object_id__
  update_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.

Since:

  • 3.0.0



26
27
28
# File 'lib/mongoid/extensions/hash.rb', line 26

def __mongoize_object_id__
  update_values(&:__mongoize_object_id__)
end

#extract_idObject

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.

Since:

  • 2.3.2



59
60
61
# File 'lib/mongoid/extensions/hash.rb', line 59

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

#mongoizeHash

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

Examples:

Mongoize the object.

object.mongoize

Returns:

  • (Hash)

    The object.

Since:

  • 3.0.0



72
73
74
# File 'lib/mongoid/extensions/hash.rb', line 72

def mongoize
  ::Hash.mongoize(self)
end

#resizable?true

Can the size of this object change?

Examples:

Is the hash resizable?

{}.resizable?

Returns:

  • (true)

    true.

Since:

  • 3.0.0



84
85
86
# File 'lib/mongoid/extensions/hash.rb', line 84

def resizable?
  true
end