Merge pull request #6 from rmk135/callable_provider

Adding callable provider with examples
This commit is contained in:
Roman 2015-01-28 00:49:55 +02:00
commit 781965ddb1
3 changed files with 118 additions and 1 deletions

View File

@ -310,3 +310,50 @@ assert (object_a2 is not object_a3) and \
(object_a2 is not object_a5) and \ (object_a2 is not object_a5) and \
(object_a2 is not object_a6) (object_a2 is not object_a6)
``` ```
Example of objects catalog with callable provider:
```python
"""
Callable provider examples.
"""
from objects import AbstractCatalog
from objects.providers import Singleton, Callable
from objects.injections import Injection, InitArg, Attribute
import sqlite3
# Some example function.
def consuming_function(arg, db):
return arg, db
# Catalog of objects providers.
class Catalog(AbstractCatalog):
"""
Objects catalog.
"""
database = Singleton(sqlite3.Connection,
InitArg('database', ':memory:'),
Attribute('row_factory', sqlite3.Row))
""" :type: (objects.Provider) -> sqlite3.Connection """
consuming_function = Callable(consuming_function,
Injection('db', database))
""" :type: (objects.Provider) -> consuming_function """
# Some calls.
arg1, db1 = Catalog.consuming_function(1)
arg2, db2 = Catalog.consuming_function(2)
arg3, db3 = Catalog.consuming_function(3)
# Some asserts.
assert db1 is db2 is db3
assert arg1 == 1
assert arg2 == 2
assert arg3 == 3
```

View File

@ -0,0 +1,42 @@
"""
Callable provider examples.
"""
from objects import AbstractCatalog
from objects.providers import Singleton, Callable
from objects.injections import Injection, InitArg, Attribute
import sqlite3
# Some example function.
def consuming_function(arg, db):
return arg, db
# Catalog of objects providers.
class Catalog(AbstractCatalog):
"""
Objects catalog.
"""
database = Singleton(sqlite3.Connection,
InitArg('database', ':memory:'),
Attribute('row_factory', sqlite3.Row))
""" :type: (objects.Provider) -> sqlite3.Connection """
consuming_function = Callable(consuming_function,
Injection('db', database))
""" :type: (objects.Provider) -> consuming_function """
# Some calls.
arg1, db1 = Catalog.consuming_function(1)
arg2, db2 = Catalog.consuming_function(2)
arg3, db3 = Catalog.consuming_function(3)
# Some asserts.
assert db1 is db2 is db3
assert arg1 == 1
assert arg2 == 2
assert arg3 == 3

View File

@ -3,7 +3,7 @@ Standard providers.
""" """
from collections import Iterable from collections import Iterable
from .injections import InitArg, Attribute, Method from .injections import Injection, InitArg, Attribute, Method
class Provider(object): class Provider(object):
@ -247,3 +247,31 @@ class Value(_StaticProvider):
""" """
Value provider provides value. Value provider provides value.
""" """
class Callable(Provider):
"""
Callable providers will provides callable calls with some predefined
dependencies injections.
"""
def __init__(self, calls, *injections):
"""
Initializer.
"""
self.calls = calls
self.injections = fetch_injections(injections, Injection)
super(Callable, self).__init__()
def __call__(self, *args, **kwargs):
"""
Returns provided instance.
"""
if self.__overridden_by__:
return self.__overridden_by__[-1].__call__(*args, **kwargs)
injections = prepare_injections(self.injections)
injections = dict(injections)
injections.update(kwargs)
return self.calls(*args, **injections)