Sha256: ac3becb0a5b934fd26b25d66824bf017e47a7da9aec0f5484bc4c01706a674bc

Contents?: true

Size: 1.19 KB

Versions: 9

Compression:

Stored size: 1.19 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Extensions #:nodoc:
    module Hash #:nodoc:

      # Expands complex criterion into mongodb selectors.
      module CriteriaHelpers

        # Expand the complex criteria into a MongoDB compliant selector hash.
        #
        # @example Convert the criterion.
        #   {}.expand_complex_criteria
        #
        # @return [ Hash ] The mongo selector.
        #
        # @since 1.0.0
        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

        # Get the id attribute from this hash, whether it's prefixed with an
        # underscore or is a symbol.
        #
        # @example Extract the id.
        #   { :_id => 1 }.extract_id
        #
        # @return [ Object ] The value of the id.
        #
        # @since 2.3.2
        def extract_id
          self["id"] || self["_id"] || self[:id] || self[:_id]
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
mongoid-multi-db-3.0.0 lib/mongoid/extensions/hash/criteria_helpers.rb
mongoid-2.3.5 lib/mongoid/extensions/hash/criteria_helpers.rb
mongoid-2.4.3 lib/mongoid/extensions/hash/criteria_helpers.rb
mongoid-2.4.2 lib/mongoid/extensions/hash/criteria_helpers.rb
mongoid-2.4.1 lib/mongoid/extensions/hash/criteria_helpers.rb
mongoid-2.4.0 lib/mongoid/extensions/hash/criteria_helpers.rb
mongoid-2.3.4 lib/mongoid/extensions/hash/criteria_helpers.rb
mongoid-2.3.3 lib/mongoid/extensions/hash/criteria_helpers.rb
mongoid-2.3.2 lib/mongoid/extensions/hash/criteria_helpers.rb