UNPKG

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

138 lines (106 loc) 3.49 kB
import Bunyan from 'bunyan'; const log = Bunyan.createLogger({name: "ClassOMatGroup"}); import uuid from 'uuid/v4'; import { CLASS_O_MAT_GROUP, NONE, DUPLICATE_UID, DUPLICATE_KEY, CLASS_O_MAT_ADDED, } from '../constants/types'; import AutonumGenerator from '../util/autonum-generator'; import ClassGroup from '../prototype/class-group'; import ClassOMat from './class-o-mat'; /** * this is a class to be internalized by the ClassOMat * even though it hosts the ids of all the ClassOMats. * */ class ClassOMatGroup { TYPE = CLASS_O_MAT_GROUP; constructor() { this._params = { uid : uuid(), classGroup : new ClassGroup(), classOMats : [], classOMatsUidIndex : {}, classOMatsKeyIndex : {} } this._autonumGenerator = new AutonumGenerator(); } $(...classOMats) { if (classOMats.length === 0) classOMats = this._params.classOMats; const { classGroup } = this._params; const Classes = classGroup.classes(); classOMats.forEach((classOMat)=>{ Classes[classOMat.id()] = classOMat.buildClass(classGroup, this); } ); classOMats.forEach((classOMat)=>{ const Class = Classes[classOMat.id()] = classOMat.addChildAccessors(classGroup) } ); classGroup.classes(Classes); return classGroup; } uid() { return this._params.uid } /** * Add a set to the group. If this set is already in * this group, then return false as isNew and its id. * If this set is part of another group, then replace the group * of that set with this group. * @param {ClassOMat} set * @return { Array} [Boolean:isNew, uint:id] */ add(classOMat) { const { classOMats, classOMatsUidIndex, classOMatsKeyIndex, uid } = this._params; const COMUid = classOMat.uid(); if (COMUid in classOMatsUidIndex === true) return [ false, COMId, DUPLICATE_UID ]; const COMKey = classOMat.key(); // if (COMKey in classOMatsKeyIndex === true) { // log.info('OUT! ') // return [ false, COMId, DUPLICATE_KEY ]; // } let COMId = classOMat.id(); if (classOMat.group() !== this.uid) { classOMat.group(this); } COMId = this._autonumGenerator.next(); classOMat.id(COMId); classOMats[COMId] = classOMat; classOMatsUidIndex[COMUid] = COMId; classOMatsKeyIndex[COMKey] = COMId; return [true, COMId, CLASS_O_MAT_ADDED]; } /** * Get the set by its id * @param {uint} setId * @return {ClassOMat} the set. */ get(classOMatId) { return this._classOMats[classOMatId] || NONE; } getByUid(classOMatUid) { const { classOMats, classOMatsUidIndex } = this._params; if (classOMatUid in classOMatsUidIndex === true) { return classOMats[classOMatsUidIndex[classOMatUid]]; } return NONE; } getByKey(classOMatKey) { const { classOMats, classOMatsKeyIndex } = this._params; if (classOMatKey in classOMatsKeyIndex === true) { return classOMats[classOMatsKeyIndex[classOMatKey]] } return NONE; } addSetChild(set, childSet) { this._classOMats[set.id()].childCount++; } setUids() { return this._params.classOMats.map((s)=>s.uid()); } } export default ClassOMatGroup;