From 94b2dee48adf2f8541f7ccb132d4715358d61ed9 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Mon, 28 Sep 2015 21:56:36 +0300 Subject: [PATCH] Add docs for usage of @inject decorator with classes --- docs/advanced_usage/index.rst | 17 +++++++- docs/main/changelog.rst | 2 +- ...ect_decorator_flask.py => inject_flask.py} | 0 .../inject_flask_class_based.py | 42 +++++++++++++++++++ ...t_decorator_simple.py => inject_simple.py} | 0 5 files changed, 58 insertions(+), 3 deletions(-) rename examples/advanced_usage/{inject_decorator_flask.py => inject_flask.py} (100%) create mode 100644 examples/advanced_usage/inject_flask_class_based.py rename examples/advanced_usage/{inject_decorator_simple.py => inject_simple.py} (100%) diff --git a/docs/advanced_usage/index.rst b/docs/advanced_usage/index.rst index 148d6d29..73459040 100644 --- a/docs/advanced_usage/index.rst +++ b/docs/advanced_usage/index.rst @@ -18,10 +18,23 @@ called to provide injectable values. Example: -.. literalinclude:: ../../examples/advanced_usage/inject_decorator_simple.py +.. literalinclude:: ../../examples/advanced_usage/inject_simple.py :language: python Example of usage ``@di.inject()`` decorator with Flask: -.. literalinclude:: ../../examples/advanced_usage/inject_decorator_flask.py +.. literalinclude:: ../../examples/advanced_usage/inject_flask.py + :language: python + + +@inject decorator with classes +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +``@di.inject()`` could be applied for classes. In such case, it will look for +class's ``__init__()`` method and pass injection to it. If decorated class has +no ``__init__()`` method, appropriate ``di.Error`` will be raised. + +Example of usage ``@di.inject()`` with Flask class-based view: + +.. literalinclude:: ../../examples/advanced_usage/inject_flask_class_based.py :language: python diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 06afa4b0..f843c622 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -11,7 +11,7 @@ follows `Semantic versioning`_ Development version ------------------- -- No featues. +- Add ``@di.inject`` functionality for decorating classes. 0.9.5 ----- diff --git a/examples/advanced_usage/inject_decorator_flask.py b/examples/advanced_usage/inject_flask.py similarity index 100% rename from examples/advanced_usage/inject_decorator_flask.py rename to examples/advanced_usage/inject_flask.py diff --git a/examples/advanced_usage/inject_flask_class_based.py b/examples/advanced_usage/inject_flask_class_based.py new file mode 100644 index 00000000..09086417 --- /dev/null +++ b/examples/advanced_usage/inject_flask_class_based.py @@ -0,0 +1,42 @@ +"""`@di.inject()` decorator with classes example.""" + +import sqlite3 +import flask +import flask.views +import dependency_injector as di + + +database = di.Singleton(sqlite3.Connection, + database=':memory:', + timeout=30, + detect_types=True, + isolation_level='EXCLUSIVE') + +app = flask.Flask(__name__) + + +@di.inject(database=database) +@di.inject(some_setting=777) +class HelloView(flask.views.View): + + """Example flask class-based view.""" + + def __init__(self, database, some_setting): + """Initializer.""" + self.database = database + self.some_setting = some_setting + + def dispatch_request(self): + """Handle example request.""" + one = self.database.execute('SELECT 1').fetchone()[0] + one *= self.some_setting + return 'Query returned {0}, db connection {1}'.format(one, database) + + +app.add_url_rule('/', view_func=HelloView.as_view('hello_view')) + +if __name__ == '__main__': + app.run() + +# Example output of "GET / HTTP/1.1" is: +# Query returned 777, db connection diff --git a/examples/advanced_usage/inject_decorator_simple.py b/examples/advanced_usage/inject_simple.py similarity index 100% rename from examples/advanced_usage/inject_decorator_simple.py rename to examples/advanced_usage/inject_simple.py