Update provider overriding docs

This commit is contained in:
Roman Mogilatov 2016-06-09 19:40:22 +03:00
parent 486353bbea
commit 91a8dbfa18
5 changed files with 13 additions and 22 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -5,7 +5,7 @@ Overriding of providers
Every provider could be overridden by another provider.
This gives opportunity to make system behaviour more flexible in some points.
This gives opportunity to make system behaviour more flexible at some point.
The main feature is that while your code is using providers, it depends on
providers, but not on the objects that providers provide. As a result of this,
you can change providing by provider object to a different one, but still
@ -14,7 +14,7 @@ compatible one, without chaning your previously written code.
Provider overriding functionality has such interface:
.. image:: /images/providers/provider_override.png
:width: 45%
:width: 55%
:align: center
+ :py:meth:`Provider.override()` - takes another provider that will be used
@ -22,17 +22,8 @@ Provider overriding functionality has such interface:
In such case, last passed provider would be used as overriding one.
+ :py:meth:`Provider.reset_override()` - resets all overriding providers.
Provider starts to behave itself like usual.
+ :py:attr:`Provider.is_overridden` - bool, ``True`` if provider is overridden.
.. note::
Actually, initial provider forms stack from overriding providers. There is
some, not so common, but still usefull, functionality that could be used:
+ :py:attr:`Provider.last_overriding` - always keeps reference to last
overriding provider.
+ :py:meth:`Provider.reset_last_overriding()` - remove last overriding
provider from stack of overriding providers.
+ :py:meth:`Provider.reset_last_overriding()` - remove last overriding
provider from stack of overriding providers.
Example:

View File

@ -13,20 +13,20 @@ class User(object):
super(User, self).__init__()
class UserService(object):
"""Example class UserService."""
class UsersService(object):
"""Example class UsersService."""
def __init__(self, user_cls):
"""Initializer."""
self.user_cls = user_cls
super(UserService, self).__init__()
super(UsersService, self).__init__()
def get_by_id(self, id):
"""Find user by his id and return user model."""
return self.user_cls(id=id, password='secret' + str(id))
# Users factory and UserService provider:
users_service = providers.Factory(UserService, user_cls=User)
# Users factory and UsersService provider:
users_service = providers.Factory(UsersService, user_cls=User)
# Getting several users and making some asserts:
user1 = users_service().get_by_id(1)
@ -58,19 +58,19 @@ class ExtendedUser(User):
super(ExtendedUser, self).__init__(id, password)
class ExtendedUserService(UserService):
"""Example class ExtendedUserService."""
class ExtendedUsersService(UsersService):
"""Example class ExtendedUsersService."""
def get_by_id(self, id):
"""Find user by his id and return user model."""
user = super(ExtendedUserService, self).get_by_id(id)
user = super(ExtendedUsersService, self).get_by_id(id)
user.first_name = 'John' + str(id)
user.last_name = 'Smith' + str(id)
user.gender = 'male'
return user
# Overriding users_service provider:
extended_users_service = providers.Factory(ExtendedUserService,
extended_users_service = providers.Factory(ExtendedUsersService,
user_cls=ExtendedUser)
users_service.override(extended_users_service)