UNPKG

jsmoo

Version:

JavaScript Minimalist Object Orientation

89 lines (69 loc) 3.21 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _has = require('./has'); var _has2 = _interopRequireDefault(_has); var _does = require('./does'); var _does2 = _interopRequireDefault(_does); var _after = require('./after'); var _before = require('./before'); 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"); } } // This is he main fail where teh construction of the JSMOO objects happends // Function to validated the initialization ATTR with the corresponding VALUE function initializeAttribute(attr, value) { var newValue = value; if (!_has.hasOptionsFor.bind(this)(attr)) throw new TypeError('The attribute ' + attr + ' is not defined'); newValue = _has.executeCoerce.bind(this)(attr, newValue); _has.typeValidation.bind(this)(attr, newValue); _has.executeTrigger.bind(this)(attr, newValue, undefined); _has.setAttribute.bind(this)(attr, newValue); } // The class that every JSMOO class has to extend off to make the magic happends when initializing. // It follows this order: // * Attach AFTER functions // * Attach BEFORE functins // * Mounts Getters and Setters // * Call beforeInitialize function if defined // * Initialize the attributes (setting the values) // * Validates REQUIRED // * Executes the DEFAULTS/BUILDERS if not LAZY // * Calls afterInitialize var Jsmoo = function Jsmoo() { var _this = this; var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(this, Jsmoo); _after.defineAfterFunctions.bind(this)(); _before.defineBeforeFunctions.bind(this)(); if (!this._has_) return; this._attributes_ = {}; this.getAttributes = _has.getAttributes.bind(this); _has.mountGettersSetters.bind(this)(); var newAttrs = attrs; if (typeof this.beforeInitialize === 'function') newAttrs = this.beforeInitialize(attrs); var hasAttr = Object.keys(_has.getAllOptions.bind(this)()); var initializedAttr = Object.keys(newAttrs).filter(function (k) { return hasAttr.indexOf(k) >= 0 ? true : false; }); initializedAttr.forEach(function (attr) { return initializeAttribute.bind(_this)(attr, newAttrs[attr]); }); _has.requireValidation.bind(this)(); hasAttr.filter(function (attr) { return initializedAttr.indexOf(attr) < 0; }).forEach(function (attr) { var value = _has.getAttribute.bind(_this)(attr); if (_has.hasOption.bind(_this)(attr, 'default') && !_has.hasOption.bind(_this)(attr, 'lazy')) { value = _has.executeDefault.bind(_this)(attr); } else if (_has.hasOption.bind(_this)(attr, 'builder') && !_has.hasOption.bind(_this)(attr, 'lazy')) { value = _has.executeBuilder.bind(_this)(attr); } value = _has.executeCoerce.bind(_this)(attr, value); _has.setAttribute.bind(_this)(attr, value); }); if (typeof this.afterInitialize === 'function') this.afterInitialize(); }; // Attach HAS and DOES functions to the Jsmoo Class Jsmoo.has = _has2.default; Jsmoo.does = _does2.default; exports.default = Jsmoo;