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
248 lines (198 loc) • 6.03 kB
JavaScript
import Bunyan from 'bunyan';
const log = Bunyan.createLogger({name: "FieldOMat"});
import mixin from 'mixin-decorator';
import clonedeep from 'lodash.clonedeep';
import { NONE, FIELD_O_MAT } from '../constants/types';
import _accessorMethods from './_accessor-methods';
import _eventMethods from './_event-methods-o-mat';
import _apiMethods from './_api-methods-o-mat';
import { getterSetter } from '../methods/field-functions';
import { getterSetter as gs, toggler as tg } from './class-o-mat-field-functions';
import FieldValidatorOMat from './field-validator-o-mat';
import FieldReducerOMat from './field-reducer-o-mat';
import FieldActionOMat from './field-action-o-mat';
import Stack from '../prototype/stack';
import Queue from '../prototype/queue';
import {build, adjustDefault} from '../methods/build-field';
const lb =(db, ...p)=>{
if (db === false) return ;
}
/**
* Class Field creates a specification for a single function to be
* added to the class to be constructed.
* This will decide what function name will get or set the field,
* what type of getter or setter and any reducers, actions or validations.
*/
class FieldOMat {
static TYPE = FIELD_O_MAT;
TYPE = FIELD_O_MAT;
cacheValidators = false;
constructor(params = NONE) {
this._params = (params === NONE)
? {
abstract : false,
actionOMats : [],
reducerOMats : [],
fallback : NONE,
upKey : '$',
_default : null,
accessorGetter : getterSetter,
isStack : false,
isQueue : false,
paramKeyPrefix : '___',
validator : NONE,
validatorOMats : [],
isRequired : false,
ON_GET : NONE,
ON_SET : NONE,
ON_DELETE : NONE,
protected : false
}
: params;
}
init(parent, key) {
const initParams = { parent, key, internalKey: key };
const params = this._params;
this._params = { ...params, ...initParams}
const { validatorOMats, actionOMats, reducerOMats } = params;
validatorOMats.forEach((validatorOMat)=>validatorOMat.connect(fieldOMat));
return this;
}
/**
* finish with this field and return to the ClassOMat
* @return {ClassOMat}
*/
$() {
return this._params.parent;
}
abstract() {
this._params.abstract = true;
return this;
}
notAbstract() {
this._params.abstract = false;
return this;
}
protected(...p) {
this._params.protected = true;
return this;
}
accessorGetter(...p) {
return gs(this, 'accessorGetter', ...p)
}
/**
* CALLED FROM THE PARENT PARAMETER SET.
* This will add the function specified by this FieldOMat
* to the Class that will be created.
* @param {Class} BaseClass
* @return {Class} BaseClass with the new field added.
*/
addToClassMixins(mixins, paramObjectKey='_params', _paramKeyPrefix = NONE) {
let workingParams = { paramObjectKey, ...this._params }
this.default(adjustDefault(this._params));
const thisAccessorMethod = build(workingParams);
const key = this._params.key;
if (Array.isArray(thisAccessorMethod) === true) {
mixins[key] = thisAccessorMethod[0];
mixins['___'+key] = thisAccessorMethod[1];
this._params.key = '___' + key;
} else {
mixins[key] = thisAccessorMethod;
}
return mixins;
}
addChild(classOMat) {
return this.$().addChild(classOMat);
}
// FIELD FACTORY DEFINITION
/**
* get a clone of this field as a new field in the same classOMat
* The new clone will be a sibling of the original FieldOMat in the param
* @param {string} key the key of the new clone
* @return {FieldOMat} The new clone
*/
again(key) {
const nextField = this.$().field(key);
const {
onSet,
_default,
accessorGetter,
isStack,
isQueue,
paramObjectKey,
validator
} = this._params;
nextField.params({
onSet,
_default,
accessorGetter,
isStack,
isQueue,
paramObjectKey,
validator
}, true);
return nextField;
}
default(...p) {
return gs(this, '_default', ...p)
}
deleteField(key) {
return this.$().field(key);
}
fallback(...p) {
return gs(this, 'fallback', ...p);
}
field(key) {
return this.$().field(key);
}
internalKey(key=NONE) {
if (key === NONE) {
return (this._params.internalKey === NONE)
? this._params.key
: this._params.internalKey;
}
this._params.internalKey = key;
return this;
}
required() {
this._params.isRequired = true;
return this;
}
// THE GETTER AND SETTER FUNCTIONS TO BE SELECTED FROM FOR THIS FIELD
// are in the _AccessorsOMat parent class
/**
* returns a FieldActionOMat to create reducers for this field.
* @return {FieldActionOMat}
*/
action() {
return new FieldActionOMat(this);
}
/**
* returns a FieldReducerOMat to create reducers for this field.
* @return {FieldReducerOMat}
*/
reducer() {
return new FieldReducerOMat(this);
}
/**
* sets the validator for this FieldOMat
* @param {Class} ValidatorOMat subclass of FieldValidatorOMat
* @return {FieldOMat} this
*/
validate(ValidatorOMat=FieldValidatorOMat) {
return new ValidatorOMat(this, this.key(), this.cacheValidators);
}
_acceptAction(actionOMat) {
this._params.actionOMats.push(actionOMat);
}
_acceptReducer(reducerOMat) {
this._params.reducerOMats.push(reducerOMat);
}
_acceptValidator(validator, validatorOMat = NONE) {
this._params.validator = validator;
this._params.validatorOMat = validatorOMat;
return this;
}
}
export default FieldOMat;