UNPKG

class-o-mat

Version:

ALPHA VERSION ONLY: class-o-mats will help to create getter-setter functionality with validations that can be attached to a container class for easier coding. Some basic validations are included. class-o-mats also has the ability to store params as bloc

69 lines (51 loc) 1.46 kB
import Bunyan from 'bunyan'; const log = Bunyan.createLogger({name: "ApiField"}); import { NONE, API_FIELD } from '../constants/types'; import {build, adjustDefault} from '../methods/build-field'; class ApiField { TYPE = API_FIELD; constructor(parent, fieldOMat, paramObjectKey, key) { this._params = { key, parent, fieldOMat, paramObjectKey, apiKey: NONE, value: NONE, _default: NONE } } $() { const { apiKey, _default, fieldOMat, parent, paramObjectKey } = this._params; const parentFields = parent.___fields; if (apiKey === NONE) throw new Error('Missing ApiField Name'); if (typeof parent.apiKey !== 'undefined' && force === false) { throw new Error(`Cannot overwrite api apiKey: ${apiKey}`); } const newFieldOMat = fieldOMat.again().key(apiKey); newFieldOMat.internalKey(apiKey); const fieldDefault = adjustDefault(newFieldOMat.params()); const fieldMethod = build({ ...newFieldOMat.params(), paramObjectKey}); parent.___fields.push(apiKey); parent[apiKey] = fieldMethod; } key() { return this._params.key; } as(apiKey) { this._params.apiKey = apiKey; return this; } as$(apiKey) { return this.as(apiKey).$(); } _default(value) { this._params._default = value; return this; } force(doForce) { this._params.force = true; return this; } } export default ApiField;