modelling/lib/ControlModel.ts

54 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-05-02 19:40:53 +02:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Modelling which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
2020-05-04 14:52:08 +02:00
import { DataApply } from './interfaces/DataApply';
2020-05-03 15:35:29 +02:00
export abstract class ControlModel implements DataApply {
2020-05-02 21:14:54 +02:00
protected data: Record<string, string|number|boolean> = {};
2020-05-02 21:11:55 +02:00
2020-05-03 15:35:29 +02:00
public constructor (obj: Record<string, string|number|boolean>) {
this.data = obj;
}
public get_data (): Record<string, string|number|boolean> {
2020-05-02 21:18:32 +02:00
return this.data;
}
2020-05-03 15:35:29 +02:00
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;
}
2020-05-02 21:18:32 +02:00
}
2020-05-03 15:35:29 +02:00
public apply (da: DataApply): void {
this.apply_object (da.get_data ());
}
public apply_to (da: DataApply): void {
da.apply (this);
2020-05-02 21:18:32 +02:00
}
2020-05-02 21:11:55 +02:00
public get (key: string): string|number|boolean {
2020-05-02 21:14:54 +02:00
return this.data[key];
2020-05-02 21:11:55 +02:00
}
public set (key: string, value: string|number|boolean): void {
this.data[key] = value;
}
2020-04-23 17:09:14 +02:00
public update (): void {
this.verify ();
}
public abstract verify(): void;
}