Skip to content

Commit

Permalink
Sets proper name on record type for identification
Browse files Browse the repository at this point in the history
Also supports a custom name on the `@Model()` decorator.
  • Loading branch information
gingi committed Nov 9, 2023
1 parent 7ccf1f5 commit 1c2cb2c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions desktop/src/@batch-flask/core/record/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ export function ListProp<T>(type: any) {
};
}

export function Model() {
export function Model(name?: string) {
return <T extends new(...args: any[]) => {}>(ctr: T) => {
if (!(ctr.prototype instanceof Record)) {
throw new RecordMissingExtendsError(ctr);
}

return (class extends ctr {
const model = (class extends ctr {
constructor(...args: any[]) {
const [data] = args;
if (data instanceof ctr) {
Expand All @@ -72,5 +71,7 @@ export function Model() {
(this as any)._completeInitialization();
}
});
Object.defineProperty(model, "name", { value: name || ctr.name });
return model;
};
}
14 changes: 14 additions & 0 deletions desktop/src/@batch-flask/core/record/record.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class InheritedTestRec extends SimpleTestRec {
public d: number;
}

@Model("CustomName")
class CustomNameRec extends Record<any> {
@Prop()
public id: string = "default-id";
}

describe("Record", () => {
it("should throw an exeption when record doesn't extends Record class", () => {
try {
Expand Down Expand Up @@ -176,4 +182,12 @@ describe("Record", () => {
expect(SimpleTestRec.isStaticMethod).not.toBeFalsy();
expect(SimpleTestRec.isStaticMethod()).toBe(true);
});

it("should allow a custom record name to be set", () => {
const rec1 = new TestRec();
expect(rec1.constructor.name).toEqual("TestRec");

const rec2 = new CustomNameRec();
expect(rec2.constructor.name).toEqual("CustomName");
});
});

0 comments on commit 1c2cb2c

Please sign in to comment.