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

74 lines (53 loc) 1.4 kB
import Bunyan from 'bunyan'; const log = Bunyan.createLogger({name: "ClassOMat"}); import { NONE, RADIO_O_MAT, RADIO_OPTION } from '../constants/types'; import FieldOMat from './field-o-mat'; class RadioOption { TYPE = RADIO_OPTION; constructor(parent, key) { this._params = { parent, key, value: NONE} } $() { return this._params.parent.$(); } value(value) { this._params.value = value; return this; } value$(value) { return this.value(value).$() } option(key) { return this._params.parent.option(key); } } class RadioOMat { TYPE = RADIO_O_MAT; constructor(parent, key) { this._params = { parent, key, baseField: NONE, options: [] } } $() { const { parent, key, baseField, options } = this._params; parent._params.fieldOMats.push(baseField); const internalKey = baseField.internalKey(); options.forEach((option)=>{ const { key, value } = option._params; parent.field(key).staticSetter(value).internalKey(internalKey); }); return parent; } base(key) { const baseField = new FieldOMat(this, key).getter(); this._params.baseField = baseField; return baseField; } option(key) { const radioOption = new RadioOption(this, key); this._params.options.push(radioOption); return radioOption; } } export default RadioOMat;