Module: Mongoid::Extensions::Hash

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary (collapse)

Instance Method Details

- (Hash) __consolidate__(klass)

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

Examples:

Consolidate the hash.

{ name: "Placebo" }.__consolidate__

Since:

  • 3.0.0



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

def __consolidate__(klass)
  consolidated = {}
  each_pair do |key, value|
    if key =~ /\$/
      value.each_pair do |_key, _value|
        value[_key] = (key == "$rename") ? _value.to_s : mongoize_for(key, klass, _key, _value)
      end
      (consolidated[key] ||= {}).merge!(value)
    else
      (consolidated["$set"] ||= {}).merge!(key => mongoize_for(key, klass, key, value))
    end
  end
  consolidated
end

- (Hash) __evolve_object_id__

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

Examples:

Convert the hash values.

{ field: id }.__evolve_object_id__

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

- (Hash) __mongoize_object_id__

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

Examples:

Convert the hash values.

{ field: id }.__mongoize_object_id__

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

- (Object) __nested__(string)

Fetch a nested value via dot syntax.

Examples:

Fetch a nested value via dot syntax.

{ "name" => { "en" => "test" }}.__nested__("name.en")

Since:

  • 3.0.15



100
101
102
103
104
105
106
107
108
# File 'lib/mongoid/extensions/hash.rb', line 100

def __nested__(string)
  keys = string.split(".")
  value = self
  keys.each do |key|
    nested = value[key] || value[key.to_i]
    value = nested
  end
  value
end

- (true, false) blank_criteria?

Check if the hash is part of a blank relation criteria.

Examples:

Is the hash blank criteria?

{}.blank_criteria?

Since:

  • 3.1.0



61
62
63
# File 'lib/mongoid/extensions/hash.rb', line 61

def blank_criteria?
  self == { "_id" => { "$in" => [] }}
end

- (Object) delete_id

Deletes an id value from the hash.

Examples:

Delete an id value.

{}.delete_id

Since:

  • 3.0.2



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

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

- (Object) extract_id

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

Since:

  • 2.3.2



86
87
88
# File 'lib/mongoid/extensions/hash.rb', line 86

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

- (Hash) mongoize

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

Examples:

Mongoize the object.

object.mongoize

Since:

  • 3.0.0



119
120
121
# File 'lib/mongoid/extensions/hash.rb', line 119

def mongoize
  ::Hash.mongoize(self)
end

- (true) resizable?

Can the size of this object change?

Examples:

Is the hash resizable?

{}.resizable?

Since:

  • 3.0.0



131
132
133
# File 'lib/mongoid/extensions/hash.rb', line 131

def resizable?
  true
end

- (Criteria) to_criteria

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

Since:

  • 3.0.7



145
146
147
148
149
150
151
# File 'lib/mongoid/extensions/hash.rb', line 145

def to_criteria
  criteria = Criteria.new(delete(:klass) || delete("klass"))
  each_pair do |method, args|
    criteria = criteria.__send__(method, args)
  end
  criteria
end