Update docs about catalogs inheritance

This commit is contained in:
Roman Mogilatov 2015-10-07 19:41:53 +03:00
parent ee73f03cb2
commit 9c661744d5
3 changed files with 39 additions and 25 deletions

View File

@ -45,16 +45,23 @@ Example:
Operating with catalog providers
--------------------------------
There are several things that could be useful for operating with catalog
providers:
``di.AbstractCatalog`` has several features that could be useful for some kind
of operations on catalog's providers:
- First of all, ``di.AbstractCatalog.providers`` attribute contains ``dict``
with all catalog providers. This dictionary could be used for any kind of
operations that could be done with providers. The only note, is that
``di.AbstractCatalog.providers`` attribute is read-only.
- Second one, ``di.AbstractCatalog.filter(provider_type=di.Provider)`` method
could be used for filtering catalog providers by provider types (for example,
for getting all ``di.Factory`` providers).
- ``di.AbstractCatalog.providers`` is read-only attribute that contains
``dict`` of all catalog providers, including providers that are inherited
from parent catalogs, where key is the name of provider and value is
provider itself.
- ``di.AbstractCatalog.cls_providers`` is read-only attribute contains ``dict``
of current catalog providers, where key is the name of provider and value is
provider itself.
- ``di.AbstractCatalog.inherited_providers`` is read-only attribute contains
``dict`` of all providers that are inherited from parent catalogs, where key
is the name of provider and value is provider itself.
- ``di.AbstractCatalog.filter(provider_type=di.Provider)`` is a class method
that could be used for filtering catalog providers by provider types
(for example, for getting all ``di.Factory`` providers).
``di.AbstractCatalog.filter()`` method use ``di.AbstractCatalog.providers``.
Example:

View File

@ -12,6 +12,7 @@ Development version
-------------------
- Add functionality for decorating classes with ``@di.inject``.
- Add enhancement for ``di.AbstractCatalog`` inheritance.
0.9.5
-----

View File

@ -3,29 +3,35 @@
import dependency_injector as di
class Catalog(di.AbstractCatalog):
class CatalogA(di.AbstractCatalog):
"""Providers catalog."""
"""Example catalog A."""
provider1 = di.Factory(object)
""":type: (di.Provider) -> object"""
provider2 = di.Factory(object)
""":type: (di.Provider) -> object"""
provider3 = di.Singleton(object)
""":type: (di.Provider) -> object"""
class CatalogB(CatalogA):
provider4 = di.Singleton(object)
"""Example catalog B."""
provider2 = di.Singleton(object)
""":type: (di.Provider) -> object"""
# Making some asserts:
assert Catalog.providers == dict(provider1=Catalog.provider1,
provider2=Catalog.provider2,
provider3=Catalog.provider3,
provider4=Catalog.provider4)
assert Catalog.filter(di.Factory) == dict(provider1=Catalog.provider1,
provider2=Catalog.provider2)
assert Catalog.filter(di.Singleton) == dict(provider3=Catalog.provider3,
provider4=Catalog.provider4)
# Making some asserts for `providers` attribute:
assert CatalogA.providers == dict(provider1=CatalogA.provider1)
assert CatalogB.providers == dict(provider1=CatalogA.provider1,
provider2=CatalogB.provider2)
# Making some asserts for `cls_providers` attribute:
assert CatalogA.cls_providers == dict(provider1=CatalogA.provider1)
assert CatalogB.cls_providers == dict(provider2=CatalogB.provider2)
# Making some asserts for `inherited_providers` attribute:
assert CatalogA.inherited_providers == dict()
assert CatalogB.inherited_providers == dict(provider1=CatalogA.provider1)
# Making some asserts for `filter()` method:
assert CatalogB.filter(di.Factory) == dict(provider1=CatalogA.provider1)
assert CatalogB.filter(di.Singleton) == dict(provider2=CatalogB.provider2)