Improved type handling.
This commit is contained in:
parent
c43b17662d
commit
bcaff6e4df
1 changed files with 16 additions and 8 deletions
|
|
@ -1,11 +1,19 @@
|
||||||
from sqlalchemy import Column, Integer, String, ForeignKey, Boolean
|
from sqlalchemy import Column, Integer, String, ForeignKey, Boolean, Enum as SQLEnum
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
from crudkit.core.base import CRUDMixin, Base
|
from crudkit.core.base import CRUDMixin, Base
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
class ObjectType(str, Enum):
|
||||||
|
ROOM = "room"
|
||||||
|
THING = "thing"
|
||||||
|
EXIT = "exit"
|
||||||
|
PLAYER = "player"
|
||||||
|
PROGRAM = "program"
|
||||||
|
|
||||||
class Dbref(Base, CRUDMixin):
|
class Dbref(Base, CRUDMixin):
|
||||||
__tablename__ = "dbref"
|
__tablename__ = "dbref"
|
||||||
|
|
||||||
type = Column(String, nullable=False)
|
type = Column(SQLEnum(ObjectType, name="object_type_enum"), nullable=False)
|
||||||
name = Column(String, nullable=False)
|
name = Column(String, nullable=False)
|
||||||
is_deleted = Column(Boolean, nullable=False, default=False)
|
is_deleted = Column(Boolean, nullable=False, default=False)
|
||||||
|
|
||||||
|
|
@ -23,20 +31,20 @@ class Dbref(Base, CRUDMixin):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"#{self.id} ({self.type}): {self.name}"
|
return f"#{self.id} ({self.type}): {self.name}"
|
||||||
|
|
||||||
def is_type(self, *types: str) -> bool:
|
def is_type(self, *types: ObjectType) -> bool:
|
||||||
return self.type in types
|
return self.type in types
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_room(self): return self.is_type("room")
|
def is_room(self): return self.is_type(ObjectType.ROOM)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_thing(self): return self.is_type("thing")
|
def is_thing(self): return self.is_type(ObjectType.THING)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_exit(self): return self.is_type("exit")
|
def is_exit(self): return self.is_type(ObjectType.EXIT)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_player(self): return self.is_type("player")
|
def is_player(self): return self.is_type(ObjectType.PLAYER)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_program(self): return self.is_type("programI ho")
|
def is_program(self): return self.is_type(ObjectType.PROGRAM)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue