[book/entities] a bit of flesh to the interface chapter stable
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Tue, 02 Mar 2010 19:11:46 +0100
branchstable
changeset 4750 875dc33551a9
parent 4749 1560d51385f0
child 4751 1a9d2c3c7f0c
[book/entities] a bit of flesh to the interface chapter
doc/book/en/development/entityclasses/interfaces.rst
doc/book/en/development/entityclasses/load-sort.rst
--- a/doc/book/en/development/entityclasses/interfaces.rst	Tue Mar 02 18:46:58 2010 +0100
+++ b/doc/book/en/development/entityclasses/interfaces.rst	Tue Mar 02 19:11:46 2010 +0100
@@ -3,18 +3,61 @@
 
 Same thing as object-oriented programming interfaces.
 
-XXX how to define a cw interface
+Definition of an interface is quite trivial. An example from cubicweb
+itself (found in cubicweb/interfaces.py):
+
+.. sourcecode:: python
+
+    class ITree(Interface):
+
+        def parent(self):
+            """returns the parent entity"""
+
+        def children(self):
+            """returns the item's children"""
+
+        def children_rql(self):
+            """XXX returns RQL to get children"""
+
+        def iterchildren(self):
+            """iterates over the item's children"""
+
+        def is_leaf(self):
+            """returns true if this node as no child"""
+
+        def is_root(self):
+            """returns true if this node has no parent"""
+
+        def root(self):
+            """returns the root object"""
+
 
 Declaration of interfaces implemented by a class
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-XXX __implements__
+.. sourcecode:: python
 
+  from cubicweb.interfaces import ITree
+  from cubicweb.mixins import TreeMixIn
+
+  class MyEntity(TreeMixIn, AnyEntity):
+      __regid__ = 'MyEntity'
+      __implements__ = AnyEntity.__implements__ + ('ITree',)
 
-Interfaces defined in the library
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+      tree_attribute = 'filed_under'
+
+The TreeMixIn here provides a default implementation for the
+interface. The tree_attribute class attribute is actually used by this
+implementation to help implement correct behaviour.
+
+Interfaces (and some implementations as mixins) defined in the library
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 .. automodule:: cubicweb.interface
    :members:
 
+.. automodule:: cubicweb.mixins
+   :members:
 
+
+
--- a/doc/book/en/development/entityclasses/load-sort.rst	Tue Mar 02 18:46:58 2010 +0100
+++ b/doc/book/en/development/entityclasses/load-sort.rst	Tue Mar 02 19:11:46 2010 +0100
@@ -24,6 +24,7 @@
   on the name attribute): ::
 
    class MyEntity(AnyEntity):
+       __regid__ = 'MyEntity'
        fetch_attrs = ('modification_date', 'name')
 
        @classmethod
@@ -45,7 +46,7 @@
 
   class Transition(AnyEntity):
     """..."""
-    id = 'Transition'
+    __regid__ = 'Transition'
     fetch_attrs, fetch_order = fetch_config(['name'])
 
 Indicates that for the entity type "Transition", you have to pre-load