[Zope3-checkins] CVS: Zope3/src/zope/proxy/context - decorators.txt:1.1.2.2
Steve Alexander
steve@cat-box.net
Sun, 18 May 2003 16:22:19 -0400
Update of /cvs-repository/Zope3/src/zope/proxy/context
In directory cvs.zope.org:/tmp/cvs-serv29652
Modified Files:
Tag: stevea-decorators-branch
decorators.txt
Log Message:
Added some more stuff, including some code examples.
=== Zope3/src/zope/proxy/context/decorators.txt 1.1.2.1 => 1.1.2.2 ===
--- Zope3/src/zope/proxy/context/decorators.txt:1.1.2.1 Sun May 18 16:15:39 2003
+++ Zope3/src/zope/proxy/context/decorators.txt Sun May 18 16:22:19 2003
@@ -243,6 +243,9 @@
* Easy to separate tests for "standalone" classes from tests of context-
dependent functionality.
+* No need to include "passthrough" properties and methods in a context-
+ dependent adapter.
+
* Reuse context-dependent functionality among different implementations.
* Transparent to clients: views and adapters can just use decorated objects
@@ -301,3 +304,103 @@
* Are there other kinds of decorator that would be useful? For example, if
the attrdict were exposed to the mixin, then it could selectively cache
results in the attrdict.
+
+Example
+=======
+
+Here is some of the code to
+zope.app.container.zopecontainer.ZopeContainerDecorator
+
+
+class ZopeContainerDecorator:
+ implements(IZopeContainer)
+
+ def __init__(self, inner, outer):
+ self.inner = inner
+ self.outer = outer
+
+ def __getitem__(self, key):
+ "See IZopeItemContainer"
+ value = self.inner[key]
+ return ContextWrapper(value, self.outer, name=key)
+
+ def get(self, key, default=None):
+ "See IZopeSimpleReadContainer"
+ value = self.inner.get(key, _marker)
+ if value is not _marker:
+ return ContextWrapper(value, self.outer, name=key)
+ else:
+ return default
+
+# This was required in ZopeContainerAdapter, but there is no need for this
+# now.
+#
+# def __contains__(self, key):
+# '''See interface IReadContainer'''
+# return key in self.inner
+
+ def values(self):
+ "See IZopeReadContainer"
+ outer = self.outer
+ result = []
+ for key, value in self.inner.items():
+ result.append(ContextWrapper(value, outer, name=key))
+ return result
+
+# def keys(self):
+# '''See interface IReadContainer'''
+# return self.inner.keys()
+
+# def __len__(self):
+# '''See interface IReadContainer'''
+# return len(self.context)
+
+ def items(self):
+ "See IZopeReadContainer"
+ outer = self.outer
+ result = []
+ for key, value in self.inner.items():
+ result.append((key, ContextWrapper(value, outer, name=key)))
+ return result
+
+# code continues...
+
+Here is the decorator directive:
+
+ <decorator
+ id="zope.app.container.contextdecorator"
+ class="zope.app.container.zopecontainer.ZopeContainerDecorator"
+ names="__getitem__ get values items setObject __delitem__ rename"
+ trusted="trusted">
+ <require permission="zope.ManageContent" attributes="rename" />
+ </decorator>
+
+Here is the content directive for Folder:
+
+<content class="zope.app.content.folder.Folder">
+ <implements interface="zope.app.interfaces.container.IContentContainer" />
+ <implements
+ interface="zope.app.interfaces.annotation.IAttributeAnnotatable"
+ />
+
+ <factory
+ id="Folder"
+ permission="zope.ManageContent"
+ title="Folder"
+ description="Minimal folder" />
+ <decorate type="context" decorator="zope.app.container.contextdecorator" />
+
+ <allow interface="zope.app.interfaces.services.service.Read" />
+ <require
+ permission="zope.ManageServices"
+ interface="zope.app.interfaces.services.service.Write"
+ />
+ <require
+ permission="zope.View"
+ interface="zope.app.interfaces.container.IReadContainer"
+ />
+ <require
+ permission="zope.ManageContent"
+ interface="zope.app.interfaces.container.IWriteContainer"
+ />
+</content>