apply methods

This commit is contained in:
Timo Hocker 2020-05-03 15:35:29 +02:00
parent 241f1dafac
commit df53948f69
3 changed files with 54 additions and 10 deletions

View File

@ -5,19 +5,36 @@
* Created by Timo Hocker <timo@scode.ovh>, May 2020 * Created by Timo Hocker <timo@scode.ovh>, May 2020
*/ */
export abstract class ControlModel { import { DataApply } from './DataApply';
export abstract class ControlModel implements DataApply {
protected data: Record<string, string|number|boolean> = {}; protected data: Record<string, string|number|boolean> = {};
public get object (): Record<string, string|number|boolean> { public constructor (obj: Record<string, string|number|boolean>) {
return this.data;
}
public set object (obj: Record<string, string|number|boolean>) {
this.data = obj; this.data = obj;
} }
public constructor (obj: Record<string, string|number|boolean>) { public get_data (): Record<string, string|number|boolean> {
this.object = obj; return this.data;
}
public apply_object (obj: Record<string, unknown>): void {
for (const key of Object.keys (obj)) {
if ([
'string',
'number',
'boolean'
].indexOf (typeof obj[key]) > -1)
this.data[key] = obj[key] as string|number|boolean;
}
}
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 { public get (key: string): string|number|boolean {

6
lib/DataApply.ts Normal file
View File

@ -0,0 +1,6 @@
export interface DataApply {
apply(da: DataApply): void;
apply_object(obj: Record<string, unknown>): void;
apply_to(da: DataApply): void;
get_data(): Record<string, unknown>;
}

View File

@ -5,7 +5,9 @@
* Created by Timo Hocker <timo@scode.ovh>, May 2020 * Created by Timo Hocker <timo@scode.ovh>, May 2020
*/ */
export abstract class DatabaseModel { import { DataApply } from './DataApply';
export abstract class DatabaseModel implements DataApply {
protected data: Record<string, string|number|boolean> = {}; protected data: Record<string, string|number|boolean> = {};
public get id (): number { public get id (): number {
@ -16,10 +18,29 @@ export abstract class DatabaseModel {
this.data.id = val; this.data.id = val;
} }
public get object (): Record<string, string|number|boolean> { public get_data (): Record<string, string|number|boolean> {
return this.data; return this.data;
} }
public apply_object (obj: Record<string, unknown>): void {
for (const key of Object.keys (obj)) {
if ([
'string',
'number',
'boolean'
].indexOf (typeof obj[key]) > -1)
this.data[key] = obj[key] as string|number|boolean;
}
}
public apply (da: DataApply): void {
this.apply_object (da.get_data ());
}
public apply_to (da: DataApply): void {
da.apply (this);
}
public constructor (id: number) { public constructor (id: number) {
this.id = id; this.id = id;
} }