doc/book/en/devrepo/entityclasses/interfaces.rst
branchstable
changeset 5394 105011657405
parent 5144 5a09bea07302
equal deleted inserted replaced
5393:875bdc0fe8ce 5394:105011657405
       
     1 Interfaces
       
     2 ----------
       
     3 
       
     4 This is the same thing as object-oriented programming `interfaces`_.
       
     5 
       
     6 .. _`interfaces`: http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
       
     7 
       
     8 Definition of an interface is quite trivial. An example from cubicweb
       
     9 itself (found in cubicweb/interfaces.py):
       
    10 
       
    11 .. sourcecode:: python
       
    12 
       
    13     class ITree(Interface):
       
    14 
       
    15         def parent(self):
       
    16             """returns the parent entity"""
       
    17 
       
    18         def children(self):
       
    19             """returns the item's children"""
       
    20 
       
    21         def children_rql(self):
       
    22             """returns RQL to get children"""
       
    23 
       
    24         def iterchildren(self):
       
    25             """iterates over the item's children"""
       
    26 
       
    27         def is_leaf(self):
       
    28             """returns true if this node as no child"""
       
    29 
       
    30         def is_root(self):
       
    31             """returns true if this node has no parent"""
       
    32 
       
    33         def root(self):
       
    34             """returns the root object"""
       
    35 
       
    36 
       
    37 Declaration of interfaces implemented by a class
       
    38 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
    39 
       
    40 .. sourcecode:: python
       
    41 
       
    42   from cubicweb.interfaces import ITree
       
    43   from cubicweb.mixins import TreeMixIn
       
    44 
       
    45   class MyEntity(TreeMixIn, AnyEntity):
       
    46       __regid__ = 'MyEntity'
       
    47       __implements__ = AnyEntity.__implements__ + ('ITree',)
       
    48 
       
    49       tree_attribute = 'filed_under'
       
    50 
       
    51 The TreeMixIn here provides a default implementation for the
       
    52 interface. The tree_attribute class attribute is actually used by this
       
    53 implementation to help implement correct behaviour.
       
    54 
       
    55 Interfaces (and some implementations as mixins) defined in the library
       
    56 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
    57 
       
    58 .. automodule:: cubicweb.interfaces
       
    59    :members:
       
    60 
       
    61 .. automodule:: cubicweb.mixins
       
    62    :members:
       
    63 
       
    64 
       
    65