Implement add_injection() for Callable, Factory & Singleton providers

This commit is contained in:
Roman Mogilatov 2016-05-16 11:16:14 +03:00
parent e2044cb560
commit a29508cb7a
3 changed files with 43 additions and 9 deletions

View File

@ -76,8 +76,10 @@ class Callable(Provider):
self.provides = provides
self.args = _parse_args_injections(args)
self.kwargs = _parse_kwargs_injections(args, kwargs)
self.args = tuple()
self.kwargs = tuple()
self.add_injections(*args, **kwargs)
super(Callable, self).__init__()
@ -89,6 +91,18 @@ class Callable(Provider):
"""
return self.args + self.kwargs
def add_injections(self, *args, **kwargs):
"""Add provider injections.
:param args: Tuple of injections.
:type args: tuple
:param kwargs: Dictionary of injections.
:type kwargs: dict
"""
self.args += _parse_args_injections(args)
self.kwargs += _parse_kwargs_injections(args, kwargs)
def _provide(self, *args, **kwargs):
"""Return provided instance.

View File

@ -106,13 +106,8 @@ class Factory(Callable):
raise Error('{0} can provide only {1} instances'.format(
self.__class__, self.__class__.provided_type))
self.attributes = tuple(injection
for injection in args
if is_attribute_injection(injection))
self.methods = tuple(injection
for injection in args
if is_method_injection(injection))
self.attributes = tuple()
self.methods = tuple()
super(Factory, self).__init__(provides, *args, **kwargs)
@ -126,6 +121,25 @@ class Factory(Callable):
"""
return self.args + self.kwargs + self.attributes + self.methods
def add_injections(self, *args, **kwargs):
"""Add provider injections.
:param args: Tuple of injections.
:type args: tuple
:param kwargs: Dictionary of injections.
:type kwargs: dict
"""
self.attributes += tuple(injection
for injection in args
if is_attribute_injection(injection))
self.methods += tuple(injection
for injection in args
if is_method_injection(injection))
super(Factory, self).add_injections(*args, **kwargs)
def _provide(self, *args, **kwargs):
"""Return provided instance.

View File

@ -11,6 +11,12 @@ Development version
-------------------
- No features.
1.17.0
------
- Add ``add_injections()`` method to ``Callable``, ``DelegatedCallable``,
``Factory``, ``DelegatedFactory``, ``Singleton`` and ``DelegatedSingleton``
providers.
1.16.8
------
- Fix some typos in introduction section of documentation.