@code-o-mat/globals
Version:
Abstract Package to support CodeOMat series of apps.
109 lines (90 loc) • 2.36 kB
JavaScript
import uuid from 'uuid/v4';
import { NONE, PROPS } from './constants/values';
import { CODE_O_MAT, O_MAT } from './constants/types';
import { assertIsOMat } from './assertions/o-mat-types';
import { assertIsKey } from './assertions/js-types';
import Cache from './caches/cache';
class OMat {
static APP = CODE_O_MAT;
static FAMILY = O_MAT;
static TYPE = O_MAT;
APP = CODE_O_MAT;
FAMILY = O_MAT;
TYPE = O_MAT;
constructor(source = NONE) {
this[PROPS]
= new Cache(
{
source,
name: NONE,
UID: uuid(),
Cache,
}
);
}
/**
* creates a clone with a new name and new UID
* @param {string|symbol} name
* @return {OMat} The new OMat
*/
again(name = NONE) {
assertIsKey(name);
const props = this[PROPS].clone();
props.merge({
name, UID:uuid()
});
return new this.constructor().props();
}
/**
* creates a clone with a new UID but the same name.
* @return {OMat} the new OMat
*/
clone() {
return this.again(this.name());
}
/**
* Getter / Setter for name.
* @param {string|symbol} name [description]
* @return {[type]} [description]
*/
name(name = NONE) {
if (name === NONE) return this[PROPS].get('name');
assertIsKey(name);
this[PROPS].set('name', name);
return this;
}
/**
* accessor for the properties cache
* if called with no arguments, will return the properties cache.
* if called with a javascript object, will create a new Properties
* cache from this object.
*
* This should not be used without considering
* that the UID of the OMat in the properties object
* will be eliminated.
* @param {object} newProps
* @return {props cache | OMat}
*/
props(newProps = NONE) {
if (newProps=== NONE) return this[PROPS];
const Cache = this[PROPS].get('Cache')
this[PROPS] = new Cache(newProps);
return this;
}
/**
* getter setter accessor for source
*/
source(source = NONE) {
if (source === NONE) return this[PROPS].get('source');
assertIsOMat(source);
this[PROPS].set('source', source)
return this;
}
/**
* getter for uid
*/
uid() {
return this[PROPS].get('UID');;
}
}
export default OMat;