Created
February 19, 2014 19:25
-
-
Save kevinjqiu/9099621 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class _AppendOnlyAttribute(object): | |
def __init__(self, name): | |
self.name = name | |
def __get__(self, instance, owner): | |
if instance: | |
return getattr(instance._history[0], self.name) | |
else: | |
return getattr(owner.__history_class__, self.name) | |
def __set__(self, instance, value): | |
prev_history_record = instance._history[0] | |
instance._history.insert( | |
0, prev_history_record.fork(**{ | |
self.name: value, | |
'id': instance.next_history_id(), | |
})) | |
def append_only_attr(name, **kwargs): | |
retval = _AppendOnlyAttribute(name) | |
if 'default_value' in kwargs: | |
retval.default_value = kwargs.pop('default_value') | |
return retval |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment