Skip to content

Commit

Permalink
Add support for thawing frozen objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Apr 22, 2022
1 parent 7d828a4 commit 6eb1551
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

- Added support for accessing `Dict` keys as attributes.
- Added a proper `repr()` implementation for `auto()` datafiles.
- Added support for "thawing" objects upon exiting the `frozen()` context manager.

## 1.3 (2022-04-09)

Expand Down
6 changes: 5 additions & 1 deletion datafiles/hooks.py
Expand Up @@ -158,7 +158,7 @@ def enabled(mapper, args) -> bool:


@contextmanager
def disabled():
def disabled(*objects):
"""Globally disable method hooks, temporarily."""
enabled = settings.HOOKS_ENABLED
if enabled:
Expand All @@ -167,3 +167,7 @@ def disabled():
yield
finally:
settings.HOOKS_ENABLED = enabled
if enabled:
for o in objects:
with suppress(AttributeError):
o.datafile.save()
24 changes: 22 additions & 2 deletions docs/utilities.md
Expand Up @@ -56,7 +56,27 @@ from .models import MyModel
instance = MyModel()

with datafiles.frozen():
instance.value = 42
instance.a = 1
instance.b = 2
instance.c = 3

instance.d = 4

```

This is useful when changes manipulate a complex object's structure in such a way that references to synchronized attributes are lost or to improve performance when making lots of changes.

### Thawing Objects

Unless `manual=True` is set, the next modification outside of the context manager will trigger a save. To do this automatically, include the objects as arguments:

```python
...

with datafiles.frozen(instance):
instance.a = 1
instance.b = 2
instance.c = 3
```

Additional modifications to the object will synchronize all changes.

2 changes: 1 addition & 1 deletion pyproject.toml
@@ -1,7 +1,7 @@
[tool.poetry]

name = "datafiles"
version = "1.4b2"
version = "1.4b3"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down

0 comments on commit 6eb1551

Please sign in to comment.