Skip to content

Commit 5ff0ef0

Browse files
committed
rename config lazy object to current_config
1 parent 7316b5f commit 5ff0ef0

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

docs/basics/application-context.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
# **Application Context**
1+
# **Injector Context**
22

33
In the complex web of dependencies within Ellar,
44
accessing the application instance without triggering circular import errors can be a daunting task.
55

6-
To address this challenge, Ellar introduces the concept of an application context,
6+
To address this challenge, Ellar introduces the concept of an injector context,
77
a powerful mechanism that facilitates smooth access to the application instance throughout various parts of the codebase.
88

9-
## **Understanding the Application Context**
9+
## **Understanding the Injector Context**
1010

11-
The application context serves as a bridge between different components of the application, making the application object
11+
The Injector context serves as a bridge between different components of the application, making the application object
1212
readily available through the application Dependency Injection (DI) container. This ensures that you can access the
1313
application instance without encountering import issues, whether it's during request handling, execution of CLI commands,
1414
or manual invocation of the context.
1515

1616
Two key elements of the application context are:
1717

1818
- **current_injector**: This proxy variable refers to the current application **injector**, providing access to any service or the application instance itself.
19-
- **config**: A lazy loader for application configuration, which is based on the `ELLAR_CONFIG_MODULE` reference or accessed through `application.config` when the application context is active.
19+
- **current_config**: A lazy loader for application configuration, which is based on the `ELLAR_CONFIG_MODULE` reference or accessed through `application.config` when the application context is active.
2020

2121
## **Integration with Click Commands**
2222

@@ -27,7 +27,7 @@ For instance, consider the following example:
2727

2828
```python
2929
import ellar_cli.click as click
30-
from ellar.app import current_injector
30+
from ellar.core import current_injector
3131
from ellar.di import injectable
3232

3333
@injectable
@@ -37,7 +37,7 @@ class MyService:
3737

3838
@click.command()
3939
@click.argument('name')
40-
@click.with_app_context
40+
@click.with_injector_context
4141
def my_command(name: str):
4242
my_service = current_injector.get(MyService)
4343
my_service.do_something()
@@ -63,8 +63,9 @@ For example, consider the following event handling setup:
6363

6464
```python
6565
from ellar.events import app_context_teardown, app_context_started
66-
from ellar.app import App, AppFactory, current_injector
67-
from ellar.threading import run_as_async
66+
from ellar.app import App, AppFactory
67+
from ellar.core import current_injector, injector_context
68+
from ellar.threading import run_as_sync
6869

6970
@app_context_started.connect
7071
async def on_context_started():
@@ -76,9 +77,9 @@ def on_context_ended():
7677

7778
app = AppFactory.create_app()
7879

79-
@run_as_async
80+
@run_as_sync
8081
async def run_app_context():
81-
async with app.application_context() as ctx:
82+
async with injector_context(app.injector) as ctx:
8283
print("-----> Context is now available <------")
8384

8485
app_instance = ctx.injector.get(App)

docs/cli/custom-commands.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ ARGUMENTS:
3838
--help Show this message and exit.
3939
```
4040

41-
## **With App Context Decorator**
41+
## **With Injector Context Decorator**
4242
The `ellar_cli.click` module includes a command decorator function called `with_injector_context`.
4343
This decorator ensures that a click command is executed within the application context,
44-
allowing `current_injector`, and `config` to have values.
44+
allowing `current_injector`, and `current_config` to have values.
4545

4646
For instance:
4747

docs/overview/exception_handling.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ it is intercepted by this middleware, which then automatically sends an appropri
1010
}
1111
```
1212
Depending on the application's configuration and the value of `DEBUG`, the exception handling behavior differs.
13-
When `config.DEBUG` is True, the exception that is raised is shown to the client for easy error debugging.
14-
However, when `config.DEBUG` is False, a 500 error is returned to the client, as illustrated below:
13+
When `current_config.DEBUG` is True, the exception that is raised is shown to the client for easy error debugging.
14+
However, when `current_config.DEBUG` is False, a 500 error is returned to the client, as illustrated below:
1515

1616
```json
1717
{

tests/test_application/test_app_context.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
from ellar.app import App
55
from ellar.common import Body, post
6-
from ellar.core import Config, config, current_injector, injector_context
6+
from ellar.core import Config, current_config, current_injector, injector_context
77
from ellar.testing import Test
88

99

@@ -25,12 +25,12 @@ async def test_current_config_fails_when_there_is_no_ellar_config_module(
2525

2626
async with injector_context(tm.create_application().injector):
2727
assert current_injector.get(App) is not None
28-
assert config.DEBUG is False
28+
assert current_config.DEBUG is False
2929

3030
assert caplog.text == ""
3131

3232
with caplog.at_level(logging.WARNING):
33-
assert config.DEBUG is False
33+
assert current_config.DEBUG is False
3434
print(caplog.text)
3535
assert (
3636
"You are trying to access app config outside app "
@@ -64,7 +64,7 @@ async def test_current_config_works(anyio_backend):
6464

6565
async with injector_context(tm.create_application().injector):
6666
app = current_injector.get(App)
67-
assert app.config.FRAMEWORK_NAME == config.FRAMEWORK_NAME
67+
assert app.config.FRAMEWORK_NAME == current_config.FRAMEWORK_NAME
6868

6969
with pytest.raises(RuntimeError):
7070
current_injector.get(App)
@@ -76,7 +76,7 @@ def add(a: Body[int], b: Body[int]):
7676
from ellar.core import current_injector
7777

7878
config_ = current_injector.get(Config)
79-
assert config_.FRAMEWORK_NAME == config.FRAMEWORK_NAME
79+
assert config_.FRAMEWORK_NAME == current_config.FRAMEWORK_NAME
8080
return a + b
8181

8282
tm = Test.create_test_module(
@@ -87,7 +87,7 @@ def add(a: Body[int], b: Body[int]):
8787
res = tm.get_test_client().post("/", json={"a": 1, "b": 4})
8888
assert res.json() == 5
8989
config_ = current_injector.get(Config)
90-
assert config_.FRAMEWORK_NAME == config.FRAMEWORK_NAME
90+
assert config_.FRAMEWORK_NAME == current_config.FRAMEWORK_NAME
9191

9292
with pytest.raises(RuntimeError):
9393
current_injector.get(Config)

0 commit comments

Comments
 (0)