import Model from "./Model"; import Validator from "./Validator"; import {getMethods} from "../Utils"; export default abstract class ModelComponent { protected readonly _model: T; private readonly _validators: { [key: string]: Validator } = {}; [key: string]: any; public constructor(model: T) { this._model = model; } public applyToModel(): void { this.init(); for (const property of this._properties) { if (!property.startsWith('_')) { (this._model as Model)[property] = this[property]; } } for (const method of getMethods(this)) { // @ts-ignore if (!method.startsWith('_') && ['init', 'setValidation'].indexOf(method) < 0 && this._model[method] === undefined) { // @ts-ignore this._model[method] = this[method]; } } } protected abstract init(): void; protected setValidation(propertyName: keyof this): Validator { const validator = new Validator(); this._validators[propertyName as string] = validator; return validator; } private get _properties(): string[] { return Object.getOwnPropertyNames(this).filter(p => !p.startsWith('_')); } }