Skip to content

Commit

Permalink
Merge pull request #45 from jetavator/issue-43-Return_type_of_UserPro…
Browse files Browse the repository at this point in the history
…perty_defaults_should_always_be_property_type

Add persist_defaults param to UserProperty
  • Loading branch information
jtv8 authored Jun 13, 2020
2 parents 66a9251 + ab096cd commit 4e01aab
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
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

0 comments on commit 4e01aab

Please sign in to comment.