refactor: memory model variable renaming

This commit is contained in:
Éloi Rivard 2024-03-31 12:11:44 +02:00
parent 006bf08b3d
commit 48d6065e1c
No known key found for this signature in database
GPG key ID: 7EDA204EA57DD184

View file

@ -17,8 +17,8 @@ class MemoryModel(Model):
def __init__(self, **kwargs): def __init__(self, **kwargs):
kwargs.setdefault("id", str(uuid.uuid4())) kwargs.setdefault("id", str(uuid.uuid4()))
self.state = {} self._state = {}
self.cache = {} self._cache = {}
for attribute, value in kwargs.items(): for attribute, value in kwargs.items():
setattr(self, attribute, value) setattr(self, attribute, value)
@ -62,7 +62,7 @@ class MemoryModel(Model):
if any( if any(
query.lower() in value.lower() query.lower() in value.lower()
for attribute in attributes for attribute in attributes
for value in cls.listify(instance.state.get(attribute, [])) for value in cls.listify(instance._state.get(attribute, []))
if isinstance(value, str) if isinstance(value, str)
) )
] ]
@ -117,7 +117,7 @@ class MemoryModel(Model):
self.delete() self.delete()
# update the id index # update the id index
self.index()[self.id] = copy.deepcopy(self.state) self.index()[self.id] = copy.deepcopy(self._state)
# update the index for each attribute # update the index for each attribute
for attribute in self.attributes: for attribute in self.attributes:
@ -134,7 +134,7 @@ class MemoryModel(Model):
mirror_attribute_index = self.attribute_index( mirror_attribute_index = self.attribute_index(
mirror_attribute, klass mirror_attribute, klass
).setdefault(self.id, set()) ).setdefault(self.id, set())
for subinstance_id in self.state.get(attribute, []): for subinstance_id in self._state.get(attribute, []):
if not subinstance_id or subinstance_id not in self.index(klass): if not subinstance_id or subinstance_id not in self.index(klass):
continue continue
@ -192,8 +192,8 @@ class MemoryModel(Model):
del self.index()[self.id] del self.index()[self.id]
def reload(self): def reload(self):
self.state = self.__class__.get(id=self.id).state self._state = self.__class__.get(id=self.id)._state
self.cache = {} self._cache = {}
def __eq__(self, other): def __eq__(self, other):
if other is None: if other is None:
@ -202,21 +202,21 @@ class MemoryModel(Model):
if not isinstance(other, MemoryModel): if not isinstance(other, MemoryModel):
return self == self.__class__.get(id=other) return self == self.__class__.get(id=other)
return self.state == other.state return self._state == other._state
def __hash__(self): def __hash__(self):
return hash(self.id) return hash(self.id)
def __getattr__(self, name): def __getattr__(self, name):
if name in self.attributes: if name in self.attributes:
return self.deserialize(name, self.cache.get(name, self.state.get(name))) return self.deserialize(name, self._cache.get(name, self._state.get(name)))
raise AttributeError() raise AttributeError()
def __setattr__(self, name, value): def __setattr__(self, name, value):
if name in self.attributes: if name in self.attributes:
self.cache[name] = value self._cache[name] = value
self.state[name] = self.serialize(value) self._state[name] = self.serialize(value)
else: else:
super().__setattr__(name, value) super().__setattr__(name, value)