modelling
This commit is contained in:
parent
373635f0db
commit
3838c8c978
@ -5,17 +5,12 @@
|
||||
* Created by Timo Hocker <timo@scode.ovh>, May 2020
|
||||
*/
|
||||
|
||||
import { DataApply } from './interfaces/DataApply';
|
||||
|
||||
export abstract class ControlModel implements DataApply {
|
||||
protected data: Record<string, string|number|boolean> = {};
|
||||
import { Assignable } from './interfaces/Assignable';
|
||||
|
||||
export abstract class ControlModel extends Assignable {
|
||||
public constructor (obj: Record<string, string|number|boolean>) {
|
||||
this.data = obj;
|
||||
}
|
||||
|
||||
public get_data (): Record<string, string|number|boolean> {
|
||||
return this.data;
|
||||
super ();
|
||||
this.data = obj as Record<string, string|number|boolean>;
|
||||
}
|
||||
|
||||
public apply_object (obj: Record<string, unknown>): 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 {
|
||||
|
@ -5,23 +5,13 @@
|
||||
* Created by Timo Hocker <timo@scode.ovh>, May 2020
|
||||
*/
|
||||
|
||||
import { DataApply } from './interfaces/DataApply';
|
||||
|
||||
export abstract class DatabaseModel implements DataApply {
|
||||
protected data: Record<string, string|number|boolean> = {};
|
||||
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<string, string|number|boolean> {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
public apply_object (obj: Record<string, unknown>): 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 {
|
||||
|
21
lib/interfaces/Assignable.ts
Normal file
21
lib/interfaces/Assignable.ts
Normal file
@ -0,0 +1,21 @@
|
||||
export abstract class Assignable {
|
||||
protected data: Record<string, unknown> = {};
|
||||
|
||||
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<string, unknown>): void {
|
||||
for (const key of Object.keys (obj))
|
||||
this.data[key] = obj[key];
|
||||
}
|
||||
|
||||
public get_data (): Record<string, unknown> {
|
||||
return this.data;
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
export interface DataApply {
|
||||
apply(da: DataApply): void;
|
||||
apply_object(obj: Record<string, unknown>): void;
|
||||
apply_to(da: DataApply): void;
|
||||
get_data(): Record<string, unknown>;
|
||||
}
|
7
lib/interfaces/Serializable.ts
Normal file
7
lib/interfaces/Serializable.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export interface Serializable {
|
||||
serialize(): string;
|
||||
}
|
||||
|
||||
export interface Deserializable {
|
||||
deserialize(str: string): Serializable;
|
||||
}
|
@ -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';
|
||||
|
Loading…
x
Reference in New Issue
Block a user