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
121 lines (104 loc) • 3.28 kB
JavaScript
import Bunyan from 'bunyan';
const log = Bunyan.createLogger({name: "AccessorsOMat"});
import { NONE, ACCESSORS_O_MAT } from '../constants/types';
import {
getter,
setter,
getterSetter,
getterSetterSafe,
staticSetter,
toggler,
swapper,
} from '../methods/field-functions';
const _accessorMethods = {
/**
* sets this field to become a simple getter in the target class
* ( NOTE: for performance reasons,
* Getter function operates as a function with no parameters,
* not as a traditional getter field )
* @return {FieldOMat} this
*/
getter(_default=NONE) {
this._params.accessorGetter = getter;
this.default(_default);
return this;
},
/**
* sets this field to become a simple setter in the target class
* ( NOTE: for performance reasons,
* Setter function operates as a function with a value parameter,
* not a
* @return {FieldOMat} this
*/
setter(_default=NONE) {
this._params.accessorGetter = setter;
this.default(_default);
return this;
},
staticSetter(staticValue=NONE) {
this._params.accessorGetter = staticSetter;
this._params.staticValue = staticValue;
return this;
},
/**
* sets this field to become a getter or setter depending upon if a
* value argument is passed in.
* If [fieldName]() is called, this is a getter,
* if [fieldName](someValue) is called, this sets the value to someValue
* NOTE: This type of function uses an internal symbol to represent a null
* value, which means that passing in falsey values ( false, 0, -x, null)
* will SET that falsey value as a parameter.
* @return {FieldOMat} this
*/
getterSetter(_default=NONE) {
this._params.accessorGetter = getterSetter;
this.default(_default);
return this;
},
/**
* sets this field to become a getter or setter depending upon if a
* value argument is passed in.. However, provides an option to trigger
* either an error or a fail gracefully when setting
* to ensure that null values return a chain.
* If [fieldName]() is called, this is a getter,
* if [fieldName](someValue) is called, this sets the value to someValue
* NOTE: This type of function uses an internal symbol to represent a null
* value, which means that passing in falsey values ( false, 0, -x, null)
* will SET that falsey value as a parameter.
* @return {FieldOMat} this
*/
getterSetterSafe(_default=NONE) {
this._params.accessorGetter = getterSetterSafe;
this.default(_default);
return this;
},
/**
* setter that will toggle true or false.
* @return {FieldOMat} this
*/
toggler(_default=true) {
this._params.accessorGetter = toggler;
this.default(_default);
return this;
},
/**
* A Setter that will return the original value.
* @return {FieldOMat} this
*/
swapper(_default=NONE) {
this._params.accessorGetter = swapper;
this.default(_default);
return this;
},
queue() {
this._params.isStack = false;
this._params.isQueue = true;
return this;
},
stack() {
this._params.isQueue = false;
this._params.isStack = true;
return this;
}
}
export default _accessorMethods;