diff --git a/docs/_providers.rst b/docs/_providers.rst index a4aa7394..87a85e97 100644 --- a/docs/_providers.rst +++ b/docs/_providers.rst @@ -1,77 +1,6 @@ Providers ========= -Static providers ----------------- - -Static providers are family of providers that return their values "as is". -There are four of static providers: ``Class``, ``Object``, ``Function`` and -``Value``. All of them has the same behaviour, but usage of anyone is -predicted by readability and providable object's type. - -Example: - -.. code-block:: python - - """Static providers example.""" - - from objects.providers import Class - from objects.providers import Object - from objects.providers import Function - from objects.providers import Value - - - cls_provider = Class(object) - assert cls_provider() is object - - object_provider = Object(object()) - assert isinstance(object_provider(), object) - - function_provider = Function(len) - assert function_provider() is len - - value_provider = Value(123) - assert value_provider() == 123 - - -Callable providers ------------------- - -``Callable`` provider is a provider that decorates particular callable with -some injections. Every call of this provider returns result of call of initial -callable. - -Example: - -.. code-block:: python - - """`Callable` providers examples.""" - - from objects.providers import Callable - from objects.providers import Singleton - - from objects.injections import KwArg - - import sqlite3 - - - def some_function(arg, database): - """Example function that has input arg and dependency on database.""" - return database.execute('SELECT @1', [arg]).fetchone()[0] - - - # Database and `ObjectA` providers. - database = Singleton(sqlite3.Connection, - KwArg('database', ':memory:')) - - some_function = Callable(some_function, - KwArg('database', database)) - - # Some asserts. - assert some_function(1) == 1 - assert some_function(2) == 2 - assert some_function(2231) == 2231 - External dependency providers ----------------------------- diff --git a/docs/providers/index.rst b/docs/providers/index.rst index 92a9a0d1..a32ce952 100644 --- a/docs/providers/index.rst +++ b/docs/providers/index.rst @@ -7,6 +7,7 @@ All providers are callable. They describe how particular objects are provided. .. toctree:: :maxdepth: 2 - :glob: - * + factory + singleton + static diff --git a/docs/providers/static.rst b/docs/providers/static.rst new file mode 100644 index 00000000..bd61029e --- /dev/null +++ b/docs/providers/static.rst @@ -0,0 +1,42 @@ +Static providers +---------------- + +Static providers are family of providers that return their values "as is". +There are four types of static providers: + + - ``Class`` + - ``Object`` + - ``Function`` + - ``Value`` + +All of them have the same behaviour, but usage of anyone is predicted by +readability and providing object's type. + +Example: + +.. code-block:: python + + """`Static` providers example.""" + + from objects.providers import Class + from objects.providers import Object + from objects.providers import Function + from objects.providers import Value + + + # Provides class - `object` + cls_provider = Class(object) + assert cls_provider() is object + + # Provides object - `object()` + object_provider = Object(object()) + assert isinstance(object_provider(), object) + + # Provides function - `len` + function_provider = Function(len) + assert function_provider() is len + + # Provides value - `123` + value_provider = Value(123) + assert value_provider() == 123 + diff --git a/examples/readme2/static_providers.py b/examples/providers/static.py similarity index 74% rename from examples/readme2/static_providers.py rename to examples/providers/static.py index 7664d59b..42675e0f 100644 --- a/examples/readme2/static_providers.py +++ b/examples/providers/static.py @@ -1,4 +1,4 @@ -"""Static providers example.""" +"""`Static` providers example.""" from objects.providers import Class from objects.providers import Object @@ -6,14 +6,18 @@ from objects.providers import Function from objects.providers import Value +# Provides class - `object` cls_provider = Class(object) assert cls_provider() is object +# Provides object - `object()` object_provider = Object(object()) assert isinstance(object_provider(), object) +# Provides function - `len` function_provider = Function(len) assert function_provider() is len +# Provides value - `123` value_provider = Value(123) assert value_provider() == 123