doc/book/en/devrepo/entityclasses/interfaces.rst
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Fri, 23 Apr 2010 17:31:46 +0200
branchstable
changeset 5394 105011657405
parent 5144 doc/book/en/development/entityclasses/interfaces.rst@5a09bea07302
permissions -rw-r--r--
[doc/book] move devweb up from development, turn development into devrepo (much better structure)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     1
Interfaces
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     2
----------
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     3
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
     4
This is the same thing as object-oriented programming `interfaces`_.
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
     5
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
     6
.. _`interfaces`: http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
2539
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
     7
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
     8
Definition of an interface is quite trivial. An example from cubicweb
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
     9
itself (found in cubicweb/interfaces.py):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    10
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    11
.. sourcecode:: python
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    12
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    13
    class ITree(Interface):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    14
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    15
        def parent(self):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    16
            """returns the parent entity"""
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    17
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    18
        def children(self):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    19
            """returns the item's children"""
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    20
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    21
        def children_rql(self):
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    22
            """returns RQL to get children"""
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    23
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    24
        def iterchildren(self):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    25
            """iterates over the item's children"""
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    26
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    27
        def is_leaf(self):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    28
            """returns true if this node as no child"""
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    29
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    30
        def is_root(self):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    31
            """returns true if this node has no parent"""
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    32
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    33
        def root(self):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    34
            """returns the root object"""
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    35
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    36
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    37
Declaration of interfaces implemented by a class
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    38
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2539
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    39
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    40
.. sourcecode:: python
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    41
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    42
  from cubicweb.interfaces import ITree
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    43
  from cubicweb.mixins import TreeMixIn
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    44
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    45
  class MyEntity(TreeMixIn, AnyEntity):
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    46
      __regid__ = 'MyEntity'
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    47
      __implements__ = AnyEntity.__implements__ + ('ITree',)
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    48
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    49
      tree_attribute = 'filed_under'
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    50
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    51
The TreeMixIn here provides a default implementation for the
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    52
interface. The tree_attribute class attribute is actually used by this
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    53
implementation to help implement correct behaviour.
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    54
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    55
Interfaces (and some implementations as mixins) defined in the library
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    56
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    57
4936
a4b772a0d801 Fixed some of the documentation warnings when building the book with sphinx.
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4750
diff changeset
    58
.. automodule:: cubicweb.interfaces
4437
21f2e01fdd6a update exemples using the 3.6 api and add/fix some sections (schema, vreg, talk about CW_MODE in concepts...). So much to do :'(
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2539
diff changeset
    59
   :members:
2539
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    60
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    61
.. automodule:: cubicweb.mixins
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    62
   :members:
2539
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    63
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    64
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    65