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

73 lines (55 loc) 1.49 kB
import Bunyan from 'bunyan'; const log = Bunyan.createLogger({name: "ApiFieldOMat"}); import { NONE, API_METHOD_O_MAT } from '../constants/types'; import FieldOMat from './field-o-mat'; import BaseApiMethod from '../prototype/api-method'; /** * This will allow a pre-determined type of field to * be added at different points in the API as determined * by the using author. */ class ApiMethodOMat { static TYPE = API_METHOD_O_MAT; TYPE = API_METHOD_O_MAT; constructor(parent, method=NONE) { this._params = { parent, key: NONE, method } } key(key=NONE) { if (key === NONE) return this._params.key; this._params.key = key; return this; } default() { return NONE } $() { return this._params.parent; } as(key) { this._params.apiKey = key; return this; } as$(key) { this.as(key).$(); } method(method) { this._params.method = method() return this._params.fieldOMat; } addToClassMixins(mixins, paramObjectKey = '_params') { const { apiKey, method, parent } = this._params; if (typeof apiKey !== 'string') { throw new Error(`Cannot add method without apiKey to ${parent.key()}`); } if (typeof method !== 'function') { throw new Error(`Tried to add illegal method to ${parent.key()} at ${apiKey}`); } mixins[apiKey] = function(...p) { return method.apply(this, p); } return mixins; } } export default ApiMethodOMat;