[Checkins] SVN: mongopersist/trunk/src/mongopersist/zope/ - Since people did not like the setitem with ``None`` key implementation, I

Stephen Richter cvs-admin at zope.org
Mon Mar 12 13:20:11 UTC 2012


Log message for revision 124576:
  - Since people did not like the setitem with ``None`` key implementation, I
    also added the ``MongoContainer.add(value, key=None)`` method, which makes
    specifying the key optional. The default implementation is to use the OID,
    if the key is None.
  
  

Changed:
  U   mongopersist/trunk/src/mongopersist/zope/container.py
  U   mongopersist/trunk/src/mongopersist/zope/interfaces.py
  U   mongopersist/trunk/src/mongopersist/zope/tests/test_container.py

-=-
Modified: mongopersist/trunk/src/mongopersist/zope/container.py
===================================================================
--- mongopersist/trunk/src/mongopersist/zope/container.py	2012-03-12 02:05:02 UTC (rev 124575)
+++ mongopersist/trunk/src/mongopersist/zope/container.py	2012-03-12 13:20:06 UTC (rev 124576)
@@ -187,6 +187,12 @@
         # We want to be as close as possible to using the Zope semantics.
         contained.setitem(self, self._real_setitem, key, value)
 
+    def add(self, value, key=None):
+        # We are already suporting ``None`` valued keys, which prompts the key
+        # to be the OID. But people felt that a more explicit interface would
+        # be better in this case.
+        self[key] = value
+
     def __delitem__(self, key):
         value = self[key]
         # First remove the parent and name from the object.

Modified: mongopersist/trunk/src/mongopersist/zope/interfaces.py
===================================================================
--- mongopersist/trunk/src/mongopersist/zope/interfaces.py	2012-03-12 02:05:02 UTC (rev 124575)
+++ mongopersist/trunk/src/mongopersist/zope/interfaces.py	2012-03-12 13:20:06 UTC (rev 124576)
@@ -100,3 +100,10 @@
 
         See pymongo's documentation for details on *args and **kwargs.
         """
+
+    def add(value, key=None):
+        """Add an object without necessarily knowing the key of the object.
+
+        It is up to the implementation to determine a key, if none is passed
+        in. One approach would be to use the object's OID.
+        """

Modified: mongopersist/trunk/src/mongopersist/zope/tests/test_container.py
===================================================================
--- mongopersist/trunk/src/mongopersist/zope/tests/test_container.py	2012-03-12 02:05:02 UTC (rev 124575)
+++ mongopersist/trunk/src/mongopersist/zope/tests/test_container.py	2012-03-12 13:20:06 UTC (rev 124576)
@@ -339,6 +339,26 @@
       True
 """
 
+def doctest_MongoContainer_add():
+    """MongoContainer: add(value, key=None)
+
+    Sometimes we just do not want to be responsible to determine the name of
+    the object to be added. This method makes this optional. The default
+    implementation assigns the OID as name:
+
+      >>> transaction.commit()
+      >>> dm.root['people'] = container.MongoContainer('person')
+      >>> dm.root['people'].add(Person(u'Stephan'))
+
+    Let's now search and receive documents as result:
+
+      >>> sorted(dm.root['people'].keys())
+      [u'...']
+      >>> stephan = dm.root['people'].values()[0]
+      >>> stephan.__name__ == str(stephan._p_oid.id)
+      True
+"""
+
 def doctest_MongoContainer_find():
     """MongoContainer: find
 



More information about the checkins mailing list