Bumped version to 0.0.5
Fixed problem where fields contained shared values by performing a shallow copy on all fields during Record instantiation. That way, each record has its own copy of the field instances, rather than the shared class-wide instance provided by the definition.
This commit is contained in:
parent
4023d46b4a
commit
7cb8bed61e
4 changed files with 28 additions and 23 deletions
|
@ -1,33 +1,31 @@
|
|||
from fields import Field, FieldWrapper
|
||||
from fields import Field
|
||||
import copy
|
||||
import pdb
|
||||
|
||||
class Model(object):
|
||||
record_identifier = ' '
|
||||
required = False
|
||||
|
||||
def __init__(self):
|
||||
self.value_dict = {}
|
||||
|
||||
for (key, value) in self.__class__.__dict__.items():
|
||||
if isinstance(value, Field):
|
||||
field = getattr(self, key)
|
||||
if not field.name:
|
||||
setattr(field, 'name', key)
|
||||
setattr(field, 'parent_name', self.__class__.__name__)
|
||||
# GRAB THE FIELD INSTANCE FROM THE CLASS DEFINITION
|
||||
# AND MAKE A LOCAL COPY FOR THIS RECORD'S INSTANCE,
|
||||
# OTHERWISE WE'LL END UP WITH VALUES BEING SHARED
|
||||
# ACROSS RECORDS.
|
||||
src_field = self.__class__.__dict__[key]
|
||||
if not src_field.name:
|
||||
setattr(src_field, 'name', key)
|
||||
setattr(src_field, 'parent_name', self.__class__.__name__)
|
||||
self.__dict__[key] = copy.copy(src_field)
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
print "Model.__setattr__", self, key, value
|
||||
if hasattr(self, key) and isinstance(getattr(self, key), Field):
|
||||
getattr(self, key).value = value
|
||||
else:
|
||||
# MAYBE THIS SHOULD RAISE A PROPERTY ERROR?
|
||||
self.__dict__[key] = value
|
||||
|
||||
def __getattribute__(self, key):
|
||||
result = object.__getattribute__(self, key)
|
||||
if isinstance(result, Field):
|
||||
return FieldWrapper(self.value_dict, result)
|
||||
return result
|
||||
|
||||
|
||||
def get_fields(self):
|
||||
fields = []
|
||||
for key in self.__class__.__dict__.keys():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue