doc/book/en/devrepo/entityclasses/adapters.rst
author Alexandre Fayolle <alexandre.fayolle@logilab.fr>
Thu, 16 Sep 2010 16:42:58 +0200
branchstable
changeset 6268 05a855ffb65d
parent 6157 81ae5bc958db
child 6324 bdb85e3602c8
permissions -rw-r--r--
force debug mode to false in service mode
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
     1
.. _adapters:
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
     2
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
     3
Interfaces and Adapters
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
     4
-----------------------
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
     6
Interfaces are the same thing as object-oriented programming
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
     7
`interfaces`_. Adapter refers to a well-known `adapter`_ design
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
     8
pattern that helps separating concerns in object oriented
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
     9
applications.
5144
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    10
5a09bea07302 [doc/book] a new chapter on how to use the ORM
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4936
diff changeset
    11
.. _`interfaces`: http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    12
.. _`adapter`: http://en.wikipedia.org/wiki/Adapter_pattern
2539
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    13
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    14
In |cubicweb| adapters provide logical functionalities
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    15
to entity types. They are introduced in version `3.9`. Before that one
6152
6824f8b61098 use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5893
diff changeset
    16
had to implement Interfaces in entity classes to achieve a similar goal. However,
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    17
hte problem with this approch is that is clutters the entity class's namespace, exposing
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    18
name collision risks with schema attributes/relations or even methods names
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    19
(different interfaces may define the same method with not necessarily the same
5882
4c7a0b139830 [doc] fix typo
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5879
diff changeset
    20
behaviour expected).
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    21
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    22
Definition of an adapter is quite trivial. An excerpt from cubicweb
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    23
itself (found in :mod:`cubicweb.entities.adapters`):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    24
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    25
.. sourcecode:: python
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    26
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    27
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    28
    class ITreeAdapter(EntityAdapter):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    29
        """This adapter has to be overriden to be configured using the
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    30
        tree_relation, child_role and parent_role class attributes to
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    31
        benefit from this default implementation
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    32
        """
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    33
        __regid__ = 'ITree'
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    34
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    35
        child_role = 'subject'
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    36
        parent_role = 'object'
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    37
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    38
        def children_rql(self):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    39
            """returns RQL to get children """
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    40
            return self.entity.cw_related_rql(self.tree_relation, self.parent_role)
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    41
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    42
The adapter object has ``self.entity`` attribute which represents the
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    43
entity being adapted.
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    44
6157
81ae5bc958db [doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    45
.. Note::
81ae5bc958db [doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    46
81ae5bc958db [doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    47
   Adapters came with the notion of service identified by the registry identifier
81ae5bc958db [doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    48
   of an adapters, hence dropping the need for explicit interface and the
81ae5bc958db [doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    49
   :class:`cubicweb.selectors.implements` selector. You should instead use
81ae5bc958db [doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    50
   :class:`cubicweb.selectors.is_instance` when you want to select on an entity
81ae5bc958db [doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    51
   type, or :class:`cubicweb.selectors.adaptable` when you want to select on a
81ae5bc958db [doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    52
   service.
81ae5bc958db [doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    53
81ae5bc958db [doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
    54
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    55
Specializing and binding an adapter
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    56
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    57
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    58
.. sourcecode:: python
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    59
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    60
  from cubicweb.entities.adapters import ITreeAdapter
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    61
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    62
  class MyEntityITreeAdapter(ITreeAdapter):
5893
4469f85ace9f [doc] proof-reading and improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5882
diff changeset
    63
      __select__ = is_instance('MyEntity')
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    64
      tree_relation = 'filed_under'
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    65
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    66
The ITreeAdapter here provides a default implementation. The
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    67
tree_relation class attribute is actually used by this implementation
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    68
to help implement correct behaviour.
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    69
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    70
Here we provide a specific implementation which will be bound for
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    71
``MyEntity`` entity type (the `adaptee`).
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    72
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    73
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    74
.. _interfaces_to_adapters:
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    75
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    76
Converting code from Interfaces/Mixins to Adapters
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    77
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    78
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    79
Here we go with a small example. Before:
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    80
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    81
.. sourcecode:: python
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    82
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    83
    from cubicweb.selectors import implements
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    84
    from cubicweb.interfaces import ITree
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    85
    from cubicweb.mixins import ITreeMixIn
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    86
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    87
    class MyEntity(ITreeMixIn, AnyEntity):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    88
        __implements__ = AnyEntity.__implements__ + (ITree,)
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
    89
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    90
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    91
    class ITreeView(EntityView):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    92
        __select__ = implements('ITree')
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    93
        def cell_call(self, row, col):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    94
            entity = self.cw_rset.get_entity(row, col)
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    95
            children = entity.children()
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    96
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    97
After:
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    98
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    99
.. sourcecode:: python
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   100
6157
81ae5bc958db [doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   101
    from cubicweb.selectors import adaptable, is_instance
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   102
    from cubicweb.entities.adapters import ITreeAdapter
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   103
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   104
    class MyEntityITreeAdapter(ITreeAdapter):
6157
81ae5bc958db [doc, adapters] drop deprecated code from examples
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6152
diff changeset
   105
        __select__ = is_instance('MyEntity')
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   106
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   107
    class ITreeView(EntityView):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   108
        __select__ = adaptable('ITree')
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   109
        def cell_call(self, row, col):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   110
            entity = self.cw_rset.get_entity(row, col)
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   111
            itree = entity.cw_adapt_to('ITree')
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   112
            children = itree.children()
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   113
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   114
As we can see, the interface/mixin duality disappears and the entity
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   115
class itself is completely freed from these concerns. When you want
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   116
to use the ITree interface of an entity, call its `cw_adapt_to` method
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   117
to get an adapter for this interface, then access to members of the
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   118
interface on the adapter
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   119
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   120
Let's look at an example where we defined everything ourselves. We
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   121
start from:
2539
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   122
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
   123
.. sourcecode:: python
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   124
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   125
    class IFoo(Interface):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   126
        def bar(self, *args):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   127
            raise NotImplementedError
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   128
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   129
    class MyEntity(AnyEntity):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   130
        __regid__ = 'MyEntity'
5893
4469f85ace9f [doc] proof-reading and improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5882
diff changeset
   131
	__implements__ = AnyEntity.__implements__ + (IFoo,)
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   132
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   133
        def bar(self, *args):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   134
            return sum(captain.age for captain in self.captains)
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
   135
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   136
    class FooView(EntityView):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   137
       __regid__ = 'mycube.fooview'
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   138
       __select__ = implements('IFoo')
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   139
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   140
        def cell_call(self, row, col):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   141
            entity = self.cw_rset.get_entity(row, col)
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   142
            self.w('bar: %s' % entity.bar())
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   143
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   144
Converting to:
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   145
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   146
.. sourcecode:: python
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
   147
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   148
   class IFooAdapter(EntityAdapter):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   149
       __regid__ = 'IFoo'
5893
4469f85ace9f [doc] proof-reading and improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5882
diff changeset
   150
       __select__ = is_instance('MyEntity')
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   151
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   152
       def bar(self, *args):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   153
           return sum(captain.age for captain in self.entity.captains)
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   154
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   155
   class FooView(EntityView):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   156
      __regid__ = 'mycube.fooview'
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   157
      __select__ = adaptable('IFoo')
4750
875dc33551a9 [book/entities] a bit of flesh to the interface chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4437
diff changeset
   158
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   159
        def cell_call(self, row, col):
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   160
            entity = self.cw_rset.get_entity(row, col)
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   161
            self.w('bar: %s' % entity.cw_adapt_to('IFoo').bar())
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   162
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   163
.. note::
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   164
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   165
   When migrating an entity method to an adapter, the code can be moved as is
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   166
   except for the `self` of the entity class, which in the adapter must become `self.entity`.
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   167
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   168
Adapters defined in the library
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   169
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   170
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   171
.. automodule:: cubicweb.entities.adapters
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
   172
   :members:
2539
0f26a76b0348 [doc] some more rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   173
5879
7d3044271a29 [doc] update book for adapters
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
   174
More are defined in web/views.