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

View File

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

View File

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