Sha256: 753ebfe93c506a699dbfadd4c9d1d94815a392719ccbe6fe9a3b3696f47feb5e

Contents?: true

Size: 935 Bytes

Versions: 15

Compression:

Stored size: 935 Bytes

Contents

module Mongoid
  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 +BSON::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 +BSON::ObjectID+s
      def string_to_object_id(id)
        if String === id
          ::BSON::ObjectID.from_string(id)
        else
          id
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
mongo_doc-0.6.11 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.6.10 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.6.9 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.6.8 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.6.7 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.6.6 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.6.5 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.6.4 lib/mongo_doc/contexts/ids.rb
mongo_doc_rails2-0.6.2 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.6.3 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.6.2 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.6.1 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.6.0 lib/mongo_doc/contexts/ids.rb
mongo_doc_rails2-0.6.1 lib/mongo_doc/contexts/ids.rb
mongo_doc-0.5.5 lib/mongo_doc/contexts/ids.rb