diff --git a/lib/ControlModel.ts b/lib/ControlModel.ts index 72caa2c..d23c97e 100644 --- a/lib/ControlModel.ts +++ b/lib/ControlModel.ts @@ -5,19 +5,36 @@ * Created by Timo Hocker , May 2020 */ -export abstract class ControlModel { +import { DataApply } from './DataApply'; + +export abstract class ControlModel implements DataApply { protected data: Record = {}; - public get object (): Record { - return this.data; - } - - public set object (obj: Record) { + public constructor (obj: Record) { this.data = obj; } - public constructor (obj: Record) { - this.object = obj; + public get_data (): Record { + return this.data; + } + + public apply_object (obj: Record): 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 { diff --git a/lib/DataApply.ts b/lib/DataApply.ts new file mode 100644 index 0000000..8cd7568 --- /dev/null +++ b/lib/DataApply.ts @@ -0,0 +1,6 @@ +export interface DataApply { + apply(da: DataApply): void; + apply_object(obj: Record): void; + apply_to(da: DataApply): void; + get_data(): Record; +} diff --git a/lib/DatabaseModel.ts b/lib/DatabaseModel.ts index b84ba56..d8d6780 100644 --- a/lib/DatabaseModel.ts +++ b/lib/DatabaseModel.ts @@ -5,7 +5,9 @@ * Created by Timo Hocker , May 2020 */ -export abstract class DatabaseModel { +import { DataApply } from './DataApply'; + +export abstract class DatabaseModel implements DataApply { protected data: Record = {}; public get id (): number { @@ -16,10 +18,29 @@ export abstract class DatabaseModel { this.data.id = val; } - public get object (): Record { + public get_data (): Record { return this.data; } + public apply_object (obj: Record): 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) { this.id = id; }