Module: Mongoid::Extensions::Hash::CriteriaHelpers

Included in:
Hash
Defined in:
lib/mongoid/extensions/hash/criteria_helpers.rb

Overview

Expands complex criterion into mongodb selectors.

Instance Method Summary collapse

Instance Method Details

#expand_complex_criteriaHash

Expand the complex criteria into a MongoDB compliant selector hash.

Examples:

Convert the criterion.

{}.expand_complex_criteria

Returns:

  • (Hash)

    The mongo selector.

Since:

  • 1.0.0



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mongoid/extensions/hash/criteria_helpers.rb', line 17

def expand_complex_criteria
  {}.tap do |hsh|
    each_pair do |k,v|
      if k.respond_to?(:key) && k.respond_to?(:to_mongo_query)
        hsh[k.key] ||= {}
        hsh[k.key].merge!(k.to_mongo_query(v))
      else
        hsh[k] = v
      end
    end
  end
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



39
40
41
# File 'lib/mongoid/extensions/hash/criteria_helpers.rb', line 39

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