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