Skip to content

Commit

Permalink
Revise magic method mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
mononobi committed Mar 8, 2021
1 parent 4712e3d commit 99ce093
Showing 1 changed file with 8 additions and 50 deletions.
58 changes: 8 additions & 50 deletions src/pyrin/database/model/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,38 +1406,11 @@ def __str__(self):
return '{fullname} -> {pk}'.format(fullname=self.get_fully_qualified_name(),
pk=self.primary_key())

@classmethod
def _set_root_base_class(cls, root_base_class):
"""
sets root base class of this entity.
root base class is the class which is direct subclass
of declarative base class (which by default is CoreEntity)
in inheritance hierarchy.
for example if you use pyrin's default CoreEntity as your base model:
{CoreEntity -> BaseEntity, A -> CoreEntity, B -> A, C -> B}
then, root base class of A, B and C is A.
if you implement a new base class named MyNewDeclarativeBase as base model:
{MyNewDeclarativeBase -> BaseEntity, A -> MyNewDeclarativeBase, B -> A, C -> B}
then, root base class of A, B and C is A.
the inheritance rule also supports multi-branch hierarchy. for example:
{CoreEntity -> BaseEntity, A -> CoreEntity, B -> A, C -> A}
then, root base class of A, B and C is A.
:param type root_base_class: root base class type.
"""

setattr(root_base_class, '_root_base_class', root_base_class)

@classmethod
def _get_root_base_class(cls):
@class_property
@fast_cache
def root_base_class(cls):
"""
gets root base class of this entity and caches it.
returns None if it's not set.
gets root base class of this entity.
root base class is the class which is direct subclass
of declarative base class (which by default is CoreEntity)
Expand All @@ -1455,28 +1428,14 @@ def _get_root_base_class(cls):
{CoreEntity -> BaseEntity, A -> CoreEntity, B -> A, C -> A}
then, root base class of A, B and C is A.
:rtype: type
"""

return getattr(cls, '_root_base_class', None)

@class_property
def root_base_class(cls):
"""
gets root base class of this entity.
root base class will be calculated once and cached.
root base class will be calculated once and cached per entity type.
:rtype: type
"""

base = cls._get_root_base_class()
if base is None:
bases = inspect.getmro(cls)
root_base_entity_index = bases.index(cls.declarative_base_class) - 1
base = bases[root_base_entity_index]
cls._set_root_base_class(base)

bases = inspect.getmro(cls)
root_base_entity_index = bases.index(cls.declarative_base_class) - 1
base = bases[root_base_entity_index]
return base

@class_property
Expand Down Expand Up @@ -1607,7 +1566,6 @@ def populate_cache(cls):
"""

temp = cls.root_base_class
temp = cls.declarative_base_class
super().populate_cache()


Expand Down

0 comments on commit 99ce093

Please sign in to comment.