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
256 lines (207 loc) • 7.75 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _bunyan = require('bunyan');
var _bunyan2 = _interopRequireDefault(_bunyan);
var _classOMatFieldFunctions = require('./class-o-mat-field-functions');
var _types = require('../constants/types');
var _fieldCopier = require('./field-copier');
var _fieldCopier2 = _interopRequireDefault(_fieldCopier);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var log = _bunyan2.default.createLogger({ name: "ChildAdder" });
/**
* manages parameters for adding a child to a ClassOMat
*/
var ChildAdder = function () {
function ChildAdder() {
_classCallCheck(this, ChildAdder);
this.TYPE = _types.CHILD_ADDER;
this._params = {
parent: _types.NONE,
childOMat: _types.NONE,
childIsSelf: _types.NONE,
copyToChildFields: [],
initializer: _types.NONE,
internalKey: _types.NONE,
accessChildAs: _types.NONE,
cacheChild: true,
childKey: _types.NONE,
namedChildren: false,
parentKey: '___parent',
singleton: false
};
}
_createClass(ChildAdder, [{
key: 'init',
value: function init(parent, childOMat) {
if (childOMat.TYPE !== _types.CLASS_O_MAT && childOMat !== _types.NONE) {
throw new Error('Invalid child ClassOMat set for ' + parent.key());
}
var params = this._params;
var childIsSelf = false;
if (childOMat === _types.NONE || childOMat.uid() === parent.uid()) {
childIsSelf = true;
childOMat = parent;
}
this._params = _extends({}, params, {
childOMat: childOMat,
childIsSelf: childIsSelf,
parent: parent
});
return this;
}
/**
* Executor function to place the child functions into the
* parentSet
* @return {ClassOMat} parent ClassOMat set
*/
}, {
key: '$',
value: function $() {
return this._params.parent._addChildFromAdder(this._params);
}
/**
* Calls executor and then adds another childAdder.
*/
}, {
key: 'addChild',
value: function addChild() {
var childOMat = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _types.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.
*/
}, {
key: 'as',
value: function as(key) {
var _params = this._params;
_params.childKey = key;
if (_params.internalKey === _types.NONE) _params.internalKey = key;
return this;
}
}, {
key: 'accessChildAs',
value: function accessChildAs() {
for (var _len = arguments.length, p = Array(_len), _key = 0; _key < _len; _key++) {
p[_key] = arguments[_key];
}
return _classOMatFieldFunctions.getterSetter.apply(undefined, [this, 'accessChildAs'].concat(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
*/
}, {
key: 'as$',
value: function as$(key) {
return this.as(key).$();
}
}, {
key: 'cacheChild',
value: function cacheChild(_cacheChild) {
if (_cacheChild === _types.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}
*/
}, {
key: 'copyField',
value: function copyField(key) {
return new _fieldCopier2.default(this, key);
}
}, {
key: 'initializer',
value: function initializer() {
var _initializer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _types.NONE;
if (_initializer === _types.NONE) return this._params.initializer;
if (typeof _initializer !== 'function') {
throw new Error('invalid initializer for addChild in classOMat \n ' + this._params.parent.key());
}
this._params.initializer = _initializer;
return this;
}
}, {
key: 'addInitializer',
value: function addInitializer() {
var initializer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _types.NONE;
if (initializer === _types.NONE) return this._params.initializer;
var currentInitializer = this._params.initializer;
if (currentInitializer === _types.NONE) {
this._params.initializer = initializer;
return this;
}
this._params.initializer = function (childInstance, self) {
for (var _len2 = arguments.length, p = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
p[_key2 - 2] = arguments[_key2];
}
currentInitializer.apply(undefined, [childInstance, self].concat(p));
initializer.apply(undefined, [childInstance, self].concat(p));
};
return this;
}
/**
* Sets up the internal key for this child function
* @param {string} key the internal
* @return {ClassOMat} this
*/
}, {
key: 'internalKey',
value: function internalKey(key) {
this._params.internalKey = key;
return this;
}
}, {
key: 'namedChildren',
value: function namedChildren() {
for (var _len3 = arguments.length, p = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
p[_key3] = arguments[_key3];
}
return _classOMatFieldFunctions.getterSetter.apply(undefined, [this, 'namedChildren'].concat(p));
}
}, {
key: 'parentKey',
value: function parentKey(key) {
this._params.parentKey = key;
return this;
}
}, {
key: 'singleton',
value: function 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
*/
}, {
key: '_addCopiedField',
value: function _addCopiedField(fieldCopier) {
var _fieldCopier$_params = fieldCopier._params,
key = _fieldCopier$_params.key,
as = _fieldCopier$_params.as;
this._params.copyToChildFields.push({ key: key, as: as });
return this;
}
}]);
return ChildAdder;
}();
exports.default = ChildAdder;
//# sourceMappingURL=child-o-mat-adder.js.map