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
184 lines (149 loc) • 4.36 kB
JavaScript
import Bunyan from 'bunyan';
const log = Bunyan.createLogger({name: "ChildAdder"});
import { getterSetter as gs, toggler as tg } from './class-o-mat-field-functions';
import { NONE, CHILD_ADDER, CLASS_O_MAT } from '../constants/types';
import FieldCopier from './field-copier';
/**
* manages parameters for adding a child to a ClassOMat
*/
class ChildAdder {
TYPE = CHILD_ADDER;
constructor() {
this._params = {
parent: NONE,
childOMat: NONE,
childIsSelf: NONE,
copyToChildFields : [],
initializer : NONE,
internalKey : NONE,
accessChildAs : NONE,
cacheChild : true,
childKey : NONE,
namedChildren : false,
parentKey : '___parent',
singleton : false
}
}
init(parent, childOMat) {
if (childOMat.TYPE !== CLASS_O_MAT && childOMat !== NONE) {
throw new Error(`Invalid child ClassOMat set for ${parent.key()}`);
}
const params = this._params;
let childIsSelf = false;
if (childOMat === NONE || childOMat.uid() === parent.uid()) {
childIsSelf = true;
childOMat = parent;
}
this._params = {
...params,
childOMat,
childIsSelf,
parent
}
return this;
}
/**
* Executor function to place the child functions into the
* parentSet
* @return {ClassOMat} parent ClassOMat set
*/
$() {
return this._params.parent._addChildFromAdder(this._params);
}
/**
* Calls executor and then adds another childAdder.
*/
addChild(childOMat=NONE) {
return this.$().addChild(childOMat);
}
/**
* sets the name of the function that will be used
* to call this child instance.
* @param {string} key the name of the function.
* @return {ClassOMat} this.
*/
as(key) {
const { _params} = this;
_params.childKey = key;
if (_params.internalKey === NONE) _params.internalKey = key;
return this;
}
accessChildAs(...p) {
return gs(this, 'accessChildAs', ...p);
}
/**
* sets the name of the function that will be used to call this
* child instance and then executes this childAdder with $
* @param {string} key
* @return {ClassOMat} the parent ClassOMat
*/
as$(key) { return this.as(key).$() }
cacheChild(cacheChild) {
if (cacheChild === NONE) return cacheChild;
this._params.cacheChild = cacheChild === true ? true : false;
return this;
}
/**
* sets up option to copy a child field from the parent
* to the instantiated child.
* @param {string} key the field name to be copied.
* @return {FieldCopier}
*/
copyField(key) {
return new FieldCopier(this, key);
}
initializer(initializer=NONE) {
if (initializer === NONE) return this._params.initializer;
if (typeof initializer !== 'function') {
throw new Error(`invalid initializer for addChild in classOMat
${this._params.parent.key()}`)
}
this._params.initializer = initializer;
return this;
}
addInitializer(initializer=NONE) {
if (initializer === NONE) return this._params.initializer;
const currentInitializer = this._params.initializer;
if (currentInitializer === NONE) {
this._params.initializer = initializer;
return this;
}
this._params.initializer =(childInstance, self, ...p)=>{
currentInitializer(childInstance, self, ...p);
initializer(childInstance, self, ...p);
}
return this;
}
/**
* Sets up the internal key for this child function
* @param {string} key the internal
* @return {ClassOMat} this
*/
internalKey(key) {
this._params.internalKey = key;
return this;
}
namedChildren(...p) {
return gs(this, 'namedChildren', ...p);
}
parentKey(key) {
this._params.parentKey = key;
return this;
}
singleton() {
this._params.singleton = true;
return this;
}
/**
* calls the necessary function on the child ClassOMat
* to effect a FieldCopier instance..
* @param {FieldCopier} the FieldCopier instance.
* @return {ClassOMat} this
*/
_addCopiedField(fieldCopier) {
const { key, as } = fieldCopier._params;
this._params.copyToChildFields.push({ key, as});
return this;
}
}
export default ChildAdder;