Sha256: 3f5393694e25630a9f9b486e183a5d59de5470d06f2b7751d7821eac6a171aac

Contents?: true

Size: 939 Bytes

Versions: 4

Compression:

Stored size: 939 Bytes

Contents

module MongoDoc
  module Contexts
    module Ids
      # Return documents based on an id search. Will handle if a single id has
      # been passed or mulitple ids.
      #
      # Example:
      #
      #   context.id_criteria([1, 2, 3])
      #
      # Returns:
      #
      # The single or multiple documents.
      def id_criteria(params)
        criteria.id(strings_to_object_ids(params))
        params.is_a?(Array) ? criteria.entries : one
      end

      protected

      # Convert ids from strings to +Mongo::ObjectID+s
      def strings_to_object_ids(ids)
        if Array === ids
          ids.map {|id| string_to_object_id(id) }
        else
          string_to_object_id(ids)
        end

      end

      # Convert ids from strings to +Mongo::ObjectID+s
      def string_to_object_id(id)
        if String === id
          ::Mongo::ObjectID.from_string(id)
        else
          id
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongo_doc-0.4.0 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.3.2 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.3.1 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.3.0 lib/mongo_doc/contexts/ids.rb