Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add persist_defaults param to UserProperty #45

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions features/dict.feature
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,7 @@ Feature: Test dictionary DOM objects
"""
example.current_address.first_line == "123 Fake Street"
example.previous_addresses[0].first_line == "123 Fake Street"
type(example.vehicles) is wysdom.dom.DOMDict
document(example.vehicles) is example
example.vehicles is example.vehicles
"""
2 changes: 1 addition & 1 deletion features/examples/modules/dict_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ class Person(UserObject):
Address,
default_function=lambda person: person.previous_addresses[0])
previous_addresses: List[Address] = UserProperty(SchemaArray(Address))
vehicles: Dict[str, Vehicle] = UserProperty(SchemaDict(Vehicle), default={})
vehicles: Dict[str, Vehicle] = UserProperty(SchemaDict(Vehicle), default={}, persist_defaults=True)
1 change: 1 addition & 0 deletions features/steps/steps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from behave import *

import wysdom
from wysdom import document, parent, key, schema

import os
Expand Down
2 changes: 1 addition & 1 deletion wysdom/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = (0, 1, 4)
VERSION = (0, 1, 5)

__version__ = '.'.join(map(str, VERSION))
31 changes: 27 additions & 4 deletions wysdom/user_objects/UserProperty.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from ..base_schema import Schema
from ..object_schema import resolve_arg_to_schema
from ..dom import DOMObject
from ..dom import DOMObject, DOMInfo, document


class UserProperty(object):
Expand Down Expand Up @@ -38,6 +38,13 @@ class UserProperty(object):
passed the :class:`~.wysdom.user_objects.UserObject` instance that
owns the property. Cannot be set in conjunction
with `default`.

:param persist_defaults: If this property is set to True and a UserProperty has either the
`default` or `default_function` property, when the UserProperty returns
a default value that value will also be explicitly stored in the underlying
data object. This is often desirable behavior if the UserProperty
returns another object and your code expects it to return the same
object instance each time it is accessed.
"""

def __init__(
Expand All @@ -46,7 +53,8 @@ def __init__(
optional: Optional[bool] = None,
name: Optional[str] = None,
default: Optional[Any] = None,
default_function: Optional[Callable] = None
default_function: Optional[Callable] = None,
persist_defaults: Optional[bool] = None
) -> None:
if default is not None or default_function is not None:
if default is not None and default_function is not None:
Expand All @@ -61,6 +69,7 @@ def __init__(
self.name = name
self.default = default
self.default_function = default_function
self.persist_defaults = persist_defaults

def __get__(
self,
Expand All @@ -72,9 +81,23 @@ def __get__(
"UserProperty is not valid as a class data descriptor")
if self.name not in instance:
if self.default_function:
return self.default_function(instance)
default_value = self.default_function(instance)
else:
default_value = self.default
if self.persist_defaults:
instance[self.name] = default_value
return instance[self.name]
elif default_value is None:
return default_value
else:
return self.default
return self.schema_type(
default_value,
DOMInfo(
document=document(instance),
parent=instance,
element_key=self.name
)
)
return instance[self.name]

def __set__(
Expand Down