Module: Mongoid::Extensions::Hash

Defined in:
build/mongoid-7.0/lib/mongoid/extensions/hash.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#__consolidate__(klass) ⇒ 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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'build/mongoid-7.0/lib/mongoid/extensions/hash.rb', line 42

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] ||= {}
      consolidated[key].update(value)
    else
      consolidated["$set"] ||= {}
      consolidated["$set"].update(key => mongoize_for(key, klass, 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 'build/mongoid-7.0/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
29
30
31
32
# File 'build/mongoid-7.0/lib/mongoid/extensions/hash.rb', line 26

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

#__nested__(string) ⇒ Object

Fetch a nested value via dot syntax.

Examples:

Fetch a nested value via dot syntax.

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

Parameters:

  • string (String)

    the dot syntax string.

Returns:

  • (Object)

    The matching value.

Since:

  • 3.0.15



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'build/mongoid-7.0/lib/mongoid/extensions/hash.rb', line 106

def __nested__(string)
  keys = string.split(".")
  value = self
  keys.each do |key|
    return nil if value.nil?
    value_for_key = value[key]
    if value_for_key.nil? && key.to_i.to_s == key
      value_for_key = value[key.to_i]
    end
    value = value_for_key
  end
  value
end

#blank_criteria?true, false

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

Examples:

Is the hash blank criteria?

{}.blank_criteria?

Returns:

  • (true, false)

    If the hash is blank criteria.

Since:

  • 3.1.0



67
68
69
# File 'build/mongoid-7.0/lib/mongoid/extensions/hash.rb', line 67

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

#delete_idObject

Deletes an id value from the hash.

Examples:

Delete an id value.

{}.delete_id

Returns:

  • (Object)

    The deleted value, or nil.

Since:

  • 3.0.2



79
80
81
# File 'build/mongoid-7.0/lib/mongoid/extensions/hash.rb', line 79

def delete_id
  delete("_id") || delete("id") || delete(:id) || delete(:_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



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

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



129
130
131
# File 'build/mongoid-7.0/lib/mongoid/extensions/hash.rb', line 129

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



141
142
143
# File 'build/mongoid-7.0/lib/mongoid/extensions/hash.rb', line 141

def resizable?
  true
end

#to_criteriaCriteria

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:

Since:

  • 3.0.7



155
156
157
158
159
160
161
# File 'build/mongoid-7.0/lib/mongoid/extensions/hash.rb', line 155

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