Commits

Andy Mikhailenko  committed 15d594a

Added MongoResultSet.ids() method.

  • Participants
  • Parent commits 2902a07

Comments (0)

Files changed (3)

File monk/__init__.py

 .. _pymongo: http://api.mongodb.org/python/current/
 
 """
-__version__ = '0.4.0'
+__version__ = '0.5.0'
 # remember to also update:
 #
 # * PKGBUILD

File monk/modeling.py

 
 
 class MongoResultSet(object):
+    """ A wrapper for pymongo cursor that wraps each item using given function
+    or class.
+
+    .. warning::
+
+       This class does not introduce caching.
+       Iterating over results exhausts the cursor.
+
+    """
     def __init__(self, cursor, wrapper):
         self._cursor = cursor
         self._wrap = wrapper
     def __getattr__(self, attr):
         return getattr(self._cursor, attr)
 
+    def ids(self):
+        """ Returns a generator with identifiers of objects in set.
+        These expressions are equivalent::
+
+            ids = (item.get_id() for item in result_set)
+
+            ids = result_set.ids()
+
+        .. warning::
+
+           This method **exhausts** the cursor, so an attempt to iterate over
+           results after calling this method will *fail*. The results are *not*
+           cached.
+
+        """
+        return (item.get_id() for item in self)
+
 #    def count(self):
 #        return self._cursor.count()
 

File unittests/test_modeling.py

         entry.save(self.db)
         assert entry.get_ref() == DBRef(self.Entry.collection, entry.get_id())
 
+    def test_result_set_ids(self):
+        self.collection.insert({'title': u'Foo'})
+        self.collection.insert({'title': u'Bar'})
+        results = self.Entry.find(self.db)
+        ids_manual = [x.get_id() for x in results]
+        # new object because caching is not supported
+        ids = self.Entry.find(self.db).ids()
+        assert ids_manual == list(ids)
+
     def test_equality(self):
         """Documents are equal if all these conditions are met: