Module: Mongoid::Extensions::Hash

Defined in:
build/mongoid-8.1/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.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'build/mongoid-8.1/lib/mongoid/extensions/hash.rb', line 37

def __consolidate__(klass)
  consolidated = {}
  each_pair do |key, value|
    if key =~ /\$/
      value.keys.each do |key2|
        value2 = value[key2]
        real_key = klass.database_field_name(key2)

        value.delete(key2) if real_key != key2
        value[real_key] = (key == "$rename") ? value2.to_s : mongoize_for(key, klass, real_key, value2)
      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.



13
14
15
# File 'build/mongoid-8.1/lib/mongoid/extensions/hash.rb', line 13

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.



23
24
25
26
27
28
29
# File 'build/mongoid-8.1/lib/mongoid/extensions/hash.rb', line 23

def __mongoize_object_id__
  if id = self['$oid']
    BSON::ObjectId.from_string(id)
  else
    transform_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.



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'build/mongoid-8.1/lib/mongoid/extensions/hash.rb', line 135

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

#_mongoid_unsatisfiable_criteria?true | false Also known as: blank_criteria?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks whether conditions given in this hash are known to be unsatisfiable, i.e., querying with this hash will always return no documents.

This method only handles condition shapes that Mongoid itself uses when it builds association queries. It does not guarantee that a false return value means the condition can produce a non-empty document set - only that if the return value is true, the condition always produces an empty document set.

Examples:

Unsatisfiable conditions

{'_id' => {'$in' => []}}._mongoid_unsatisfiable_criteria?
# => true

Conditions which could be satisfiable

{'_id' => '123'}._mongoid_unsatisfiable_criteria?
# => false

Conditions which are unsatisfiable that this method does not handle

{'foo' => {'$in' => []}}._mongoid_unsatisfiable_criteria?
# => false

Returns:

  • (true | false)

    Whether hash contains known unsatisfiable conditions.



83
84
85
86
87
88
89
90
91
# File 'build/mongoid-8.1/lib/mongoid/extensions/hash.rb', line 83

def _mongoid_unsatisfiable_criteria?
  unsatisfiable_criteria = { "_id" => { "$in" => [] }}
  return true if self == unsatisfiable_criteria
  return false unless length == 1 && keys == %w($and)
  value = values.first
  value.is_a?(Array) && value.any? do |sub_v|
    sub_v.is_a?(Hash) && sub_v._mongoid_unsatisfiable_criteria?
  end
end

#delete_idObject

Deletes an id value from the hash.

Examples:

Delete an id value.

{}.delete_id

Returns:

  • (Object)

    The deleted value, or nil.



112
113
114
# File 'build/mongoid-8.1/lib/mongoid/extensions/hash.rb', line 112

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.



123
124
125
# File 'build/mongoid-8.1/lib/mongoid/extensions/hash.rb', line 123

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.



156
157
158
# File 'build/mongoid-8.1/lib/mongoid/extensions/hash.rb', line 156

def mongoize
  ::Hash.mongoize(self)
end

#resizable?true

Can the size of this object change?

Examples:

Is the hash resizable?

{}.resizable?

Returns:

  • (true)

    true.



166
167
168
# File 'build/mongoid-8.1/lib/mongoid/extensions/hash.rb', line 166

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:



178
179
180
181
182
183
184
# File 'build/mongoid-8.1/lib/mongoid/extensions/hash.rb', line 178

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