module Mongoid::Extensions::Hash

def __consolidate__(klass)

Returns:
  • (Hash) - A new consolidated hash.

Other tags:
    Example: Consolidate the hash. -
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

def __evolve_object_id__

Returns:
  • (Hash) - The converted hash.

Other tags:
    Example: Convert the hash values. -
def __evolve_object_id__
  transform_values!(&:__evolve_object_id__)
end

def __mongoize_object_id__

Returns:
  • (Hash) - The converted hash.

Other tags:
    Example: Convert the hash values. -
def __mongoize_object_id__
  if id = self['$oid']
    BSON::ObjectId.from_string(id)
  else
    transform_values!(&:__mongoize_object_id__)
  end
end

def __nested__(string)

Returns:
  • (Object) - The matching value.

Parameters:
  • string (String) -- the dot syntax string.

Other tags:
    Example: Fetch a nested value via dot syntax. -
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

def _mongoid_unsatisfiable_criteria?

Other tags:
    Api: - private

Returns:
  • (true | false) - Whether hash contains known unsatisfiable

Other tags:
    Example: Conditions which are unsatisfiable that this method does not handle -
    Example: Conditions which could be satisfiable -
    Example: Unsatisfiable conditions -
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

def delete_id

Returns:
  • (Object) - The deleted value, or nil.

Other tags:
    Example: Delete an id value. -
def delete_id
  delete("_id") || delete(:_id) || delete("id") || delete(:id)
end

def extract_id

Returns:
  • (Object) - The value of the id.

Other tags:
    Example: Extract the id. -
def extract_id
  self["_id"] || self[:_id] || self["id"] || self[:id]
end

def mongoize

Returns:
  • (Hash | nil) - The object mongoized or nil.

Other tags:
    Example: Mongoize the object. -
def mongoize
  ::Hash.mongoize(self)
end

def mongoize_for(operator, klass, key, value)

Returns:
  • (Object) - The mongoized value.

Parameters:
  • value (Object) -- The value to mongoize.
  • key (String | Symbol) -- The field key.
  • klass (Class) -- The model class.
  • operator (String) -- The operator.

Other tags:
    Example: Mongoize for the klass, field and value. -

Other tags:
    Api: - private
def mongoize_for(operator, klass, key, value)
  field = klass.fields[key.to_s]
  if field
    val = field.mongoize(value)
    if Mongoid::Persistable::LIST_OPERATIONS.include?(operator) && field.resizable?
      val = val.first if !value.is_a?(Array)
    end
    val
  else
    value
  end
end

def resizable?

Returns:
  • (true) - true.

Other tags:
    Example: Is the hash resizable? -
def resizable?
  true
end

def to_criteria

Returns:
  • (Criteria) - The criteria.

Other tags:
    Example: Convert the hash to a criteria. -
def to_criteria
  criteria = Criteria.new(delete(:klass) || delete("klass"))
  each_pair do |method, args|
    criteria = criteria.__send__(method, args)
  end
  criteria
end