From 3838c8c97852f4fe99aed7cfb2c34044e855de5b Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Mon, 4 May 2020 20:04:38 +0200 Subject: [PATCH] modelling --- lib/ControlModel.ts | 25 ++++++------------------- lib/DatabaseModel.ts | 29 ++++++----------------------- lib/interfaces/Assignable.ts | 21 +++++++++++++++++++++ lib/interfaces/DataApply.ts | 6 ------ lib/interfaces/Serializable.ts | 7 +++++++ lib/interfaces/index.ts | 5 ++--- package.json | 2 +- 7 files changed, 43 insertions(+), 52 deletions(-) create mode 100644 lib/interfaces/Assignable.ts delete mode 100644 lib/interfaces/DataApply.ts create mode 100644 lib/interfaces/Serializable.ts diff --git a/lib/ControlModel.ts b/lib/ControlModel.ts index 216a203..0fe7efc 100644 --- a/lib/ControlModel.ts +++ b/lib/ControlModel.ts @@ -5,17 +5,12 @@ * Created by Timo Hocker , May 2020 */ -import { DataApply } from './interfaces/DataApply'; - -export abstract class ControlModel implements DataApply { - protected data: Record = {}; +import { Assignable } from './interfaces/Assignable'; +export abstract class ControlModel extends Assignable { public constructor (obj: Record) { - this.data = obj; - } - - public get_data (): Record { - return this.data; + super (); + this.data = obj as Record; } public apply_object (obj: Record): void { @@ -25,20 +20,12 @@ export abstract class ControlModel implements DataApply { 'number', 'boolean' ].indexOf (typeof obj[key]) > -1) - this.data[key] = obj[key] as string|number|boolean; + this.data[key] = obj[key]; } } - public apply (da: DataApply): void { - this.apply_object (da.get_data ()); - } - - public apply_to (da: DataApply): void { - da.apply (this); - } - public get (key: string): string|number|boolean { - return this.data[key]; + return this.data[key] as string|number|boolean; } public set (key: string, value: string|number|boolean): void { diff --git a/lib/DatabaseModel.ts b/lib/DatabaseModel.ts index 7aef765..ddf0ec2 100644 --- a/lib/DatabaseModel.ts +++ b/lib/DatabaseModel.ts @@ -5,23 +5,13 @@ * Created by Timo Hocker , May 2020 */ -import { DataApply } from './interfaces/DataApply'; - -export abstract class DatabaseModel implements DataApply { - protected data: Record = {}; +import { Assignable } from './interfaces/Assignable'; +export abstract class DatabaseModel extends Assignable { public get id (): number { return this.data.id as number; } - public set id (val: number) { - this.data.id = val; - } - - public get_data (): Record { - return this.data; - } - public apply_object (obj: Record): void { for (const key of Object.keys (obj)) { if ([ @@ -29,24 +19,17 @@ export abstract class DatabaseModel implements DataApply { 'number', 'boolean' ].indexOf (typeof obj[key]) > -1) - this.data[key] = obj[key] as string|number|boolean; + this.data[key] = obj[key]; } } - public apply (da: DataApply): void { - this.apply_object (da.get_data ()); - } - - public apply_to (da: DataApply): void { - da.apply (this); - } - public constructor (id: number) { - this.id = id; + super (); + this.data.id = id; } public get (key: string): string|number|boolean { - return this.data[key]; + return this.data[key] as string|number|boolean; } public set (key: string, value: string|number|boolean): void { diff --git a/lib/interfaces/Assignable.ts b/lib/interfaces/Assignable.ts new file mode 100644 index 0000000..f9f9ab9 --- /dev/null +++ b/lib/interfaces/Assignable.ts @@ -0,0 +1,21 @@ +export abstract class Assignable { + protected data: Record = {}; + + public assign (da: Assignable): void { + this.assign_object (da.get_data ()); + } + + + public assign_to (da: Assignable): void { + da.assign (this); + } + + public assign_object (obj: Record): void { + for (const key of Object.keys (obj)) + this.data[key] = obj[key]; + } + + public get_data (): Record { + return this.data; + } +} diff --git a/lib/interfaces/DataApply.ts b/lib/interfaces/DataApply.ts deleted file mode 100644 index 8cd7568..0000000 --- a/lib/interfaces/DataApply.ts +++ /dev/null @@ -1,6 +0,0 @@ -export interface DataApply { - apply(da: DataApply): void; - apply_object(obj: Record): void; - apply_to(da: DataApply): void; - get_data(): Record; -} diff --git a/lib/interfaces/Serializable.ts b/lib/interfaces/Serializable.ts new file mode 100644 index 0000000..cf75ae6 --- /dev/null +++ b/lib/interfaces/Serializable.ts @@ -0,0 +1,7 @@ +export interface Serializable { + serialize(): string; +} + +export interface Deserializable { + deserialize(str: string): Serializable; +} diff --git a/lib/interfaces/index.ts b/lib/interfaces/index.ts index 4ef9914..a7cbd62 100644 --- a/lib/interfaces/index.ts +++ b/lib/interfaces/index.ts @@ -1,3 +1,2 @@ -export { DataApply } from './DataApply'; -export { Serializable } from './Serializable'; -export { Graphable } from './Graphable'; +export { Assignable } from './Assignable'; +export { Serializable, Deserializable } from './Serializable'; diff --git a/package.json b/package.json index 6b097b2..9f4ce22 100644 --- a/package.json +++ b/package.json @@ -19,4 +19,4 @@ "/dist/", "LICENSE" ] -} \ No newline at end of file +}