Skip to content

Commit

Permalink
weakmap implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Aug 11, 2015
1 parent d9b952f commit 98798ef
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 180 deletions.
6 changes: 3 additions & 3 deletions es5/lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var _jargon = require("jargon");

Expand All @@ -25,6 +25,8 @@ var _modelFinderJs = require("./modelFinder.js");
var _modelFinderJs2 = _interopRequireDefault(_modelFinderJs);

var Collection = (function (_Array) {
_inherits(Collection, _Array);

function Collection(initialData) {
_classCallCheck(this, Collection);

Expand Down Expand Up @@ -52,8 +54,6 @@ var Collection = (function (_Array) {
});
}

_inherits(Collection, _Array);

_createClass(Collection, [{
key: "push",
value: function push() {
Expand Down
9 changes: 7 additions & 2 deletions es5/lib/model/addAssociation.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ var _collectionJs = require("../collection.js");

var _collectionJs2 = _interopRequireDefault(_collectionJs);

var _incognito = require("incognito");

var _incognito2 = _interopRequireDefault(_incognito);

//private functions

var _ = require("./");

var _2 = _interopRequireDefault(_);

//private functions
function getSetterFunctionHasOne(association, privateAssociationName, privateImplicitAssociationName) {
var _this = this;

Expand Down Expand Up @@ -236,7 +241,7 @@ function addAssociation(associationDetails) {

this[associationDetails.name] = associationDetails.value;

this._associations[associationDetails.name] = association;
(0, _incognito2["default"])(this)._associations[associationDetails.name] = association;

return associationSetter;
}
Expand Down
16 changes: 11 additions & 5 deletions es5/lib/model/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ var _jargon = require("jargon");

var _jargon2 = _interopRequireDefault(_jargon);

var _incognito = require("incognito");

var _incognito2 = _interopRequireDefault(_incognito);

var _modelFinderJs = require("../modelFinder.js");

var _modelFinderJs2 = _interopRequireDefault(_modelFinderJs);
Expand Down Expand Up @@ -181,8 +185,8 @@ function fetchByBelongsTo(associationName, associations, callback) {
function fetchBy() {
var _this5 = this;

var fields = arguments[0] === undefined ? [this.primaryKey] : arguments[0];
var callback = arguments[1] === undefined ? undefined : arguments[1];
var fields = arguments.length <= 0 || arguments[0] === undefined ? [this.primaryKey] : arguments[0];
var callback = arguments.length <= 1 || arguments[1] === undefined ? undefined : arguments[1];

if (!this.constructor.database) {
throw new Error("Cannot fetch without Model.database set.");
Expand All @@ -201,7 +205,9 @@ function fetchBy() {
}
}, this);

if (this._softDelete) {
var _ = (0, _incognito2["default"])(this);

if (_._softDelete) {
chain = chain.whereNull((0, _jargon2["default"])("deletedAt").snake.toString());
}

Expand All @@ -211,14 +217,14 @@ function fetchBy() {
} else {
_this5[_symbols2["default"].parseAttributesFromFields](records[0]);

if (_this5._includeAssociations.length > 0) {
if (_._includeAssociations.length > 0) {
(function () {
var associations = _this5.associations;

/* We'll be putting all of our Async tasks into this */
var fetchTasks = [];

_this5._includeAssociations.forEach(function (associationName) {
_._includeAssociations.forEach(function (associationName) {

var association = associations[associationName];

Expand Down
102 changes: 45 additions & 57 deletions es5/lib/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ var _quirk = require("quirk");

var _quirk2 = _interopRequireDefault(_quirk);

var _incognito = require("incognito");

var _incognito2 = _interopRequireDefault(_incognito);

var _symbols = require("./symbols");

var _symbols2 = _interopRequireDefault(_symbols);

/**
* @class Model
*/

//import additional methods from external files

var _fetchJs = require("./fetch.js");
Expand All @@ -50,10 +58,6 @@ var _modelFinderJs = require("../modelFinder.js");

var _modelFinderJs2 = _interopRequireDefault(_modelFinderJs);

/**
* @class Model
*/

var Model = (function () {
/**
* @param {Object.<String,*>} [initialAttributes] Provide default values for attributes by passing a Key-Value Object.
Expand All @@ -65,22 +69,18 @@ var Model = (function () {

_classCallCheck(this, Model);

["_validations", "_associations"].forEach(function (privatePropertyName) {
Object.defineProperty(_this, privatePropertyName, {
writable: true,
enumerable: false,
value: {}
});
});
var _ = (0, _incognito2["default"])(this);
_._validations = {};
_._associations = {};
_._includeAssociations = [];
_._tableName = null;
_._primaryKey = null;
_._softDelete = null;

/**
* Define dynamic properties
*/
Object.defineProperties(this, {
"_includeAssociations": {
enumerable: false,
writable: true,
value: []
},

"additionalAttributes": {
enumerable: false, //this fix db related issues
Expand Down Expand Up @@ -110,43 +110,27 @@ var Model = (function () {
get: this[_symbols2["default"].validations]
},

"_tableName": {
enumerable: false,
writable: true
},

"tableName": {
get: function get() {
return _this._tableName || (0, _jargon2["default"])(_this.constructor.name).plural.snake.toString();
return _._tableName || (0, _jargon2["default"])(_this.constructor.name).plural.snake.toString();
},
set: function set(newTableName) {
_this._tableName = newTableName;
_._tableName = newTableName;
}
},

"_primaryKey": {
enumerable: false,
writable: true
},

"primaryKey": {
get: function get() {
return _this._primaryKey || "id";
return _._primaryKey || "id";
},
set: function set(newPrimaryKey) {
_this._primaryKey = newPrimaryKey;
_._primaryKey = newPrimaryKey;
}
},

"_softDelete": {
enumerable: false,
writable: true,
value: false
},

"softDelete": {
get: function get() {
_this._softDelete = true;
_._softDelete = true;
}
}
});
Expand Down Expand Up @@ -192,32 +176,31 @@ var Model = (function () {
}, {
key: "ensure",
value: function ensure(attributeName, validatorFunction, validatorMessage) {
this._validations[attributeName] = this._validations[attributeName] || [];
var _ = (0, _incognito2["default"])(this);
_._validations[attributeName] = _._validations[attributeName] || [];

var validatorDetails = { validator: validatorFunction };

if (validatorMessage) {
validatorDetails.message = validatorMessage;
}

this._validations[attributeName].push(validatorDetails);
_._validations[attributeName].push(validatorDetails);
}
}, {
key: "isValid",

/**
* Return a boolean indicating whether the model is valid or not.
*
* @method isValid
* @param {Function(boolean)} callback Callback returning the boolean.
*/
}, {
key: "isValid",
value: function isValid(callback) {
this.invalidAttributes(function (invalidAttributeList) {
callback(Object.keys(invalidAttributeList).length === 0);
});
}
}, {
key: "invalidAttributes",

/**
* Return an object containing all invalid attributes and their errors.
Expand All @@ -232,10 +215,13 @@ var Model = (function () {
* @method invalidAttributes
* @param {Function(invalidAttributeList)} callback Callback returning the invalid attribute list.
*/
}, {
key: "invalidAttributes",
value: function invalidAttributes(callback) {
var _this2 = this;

var attributeNamesWithValidators = Object.keys(this._validations);
var _ = (0, _incognito2["default"])(this);
var attributeNamesWithValidators = Object.keys(_._validations);

var compileInvalidAttributeList = function compileInvalidAttributeList(errors, validatorMessages) {
if (errors) {
Expand All @@ -257,7 +243,7 @@ var Model = (function () {
};

var performValidationsForAttribute = function performValidationsForAttribute(attributeName, done) {
var attributeValidations = _this2._validations[attributeName];
var attributeValidations = _._validations[attributeName];

var performValidation = function performValidation(validation, returnValue) {
var validator = validation.validator;
Expand Down Expand Up @@ -314,15 +300,15 @@ var Model = (function () {
associationNames[_key] = arguments[_key];
}

this._includeAssociations = associationNames;
(0, _incognito2["default"])(this)._includeAssociations = associationNames;
return this;
}
}, {
key: "delete",
value: function _delete(callback) {
var _this3 = this;

if (this._softDelete) {
if ((0, _incognito2["default"])(this)._softDelete) {
if (!this.constructor.database) {
throw new Error("Cannot delete without Model.database set.");
}
Expand Down Expand Up @@ -355,10 +341,10 @@ var Model = (function () {
throw new Error("Not implemented.");
}
}
}, {
key: "beforeValidation",

/* Stubbed methods for hooks */
}, {
key: "beforeValidation",
value: function beforeValidation(callback) {
callback();
}
Expand Down Expand Up @@ -386,20 +372,20 @@ var Model = (function () {
value: function toJSON() {
return this.attributes;
}
}, {
key: _symbols2["default"].setAttributes,

/**
* Private Functionality
*/

}, {
key: _symbols2["default"].setAttributes,
value: function value(newAttributes) {
this[_symbols2["default"].parseAttributesFromFields](newAttributes);
}
}, {
key: _symbols2["default"].associations,
value: function value() {
return this._associations;
return (0, _incognito2["default"])(this)._associations;
}
}, {
key: _symbols2["default"].properties,
Expand All @@ -409,7 +395,7 @@ var Model = (function () {
}, {
key: _symbols2["default"].validations,
value: function value() {
return this._validations;
return (0, _incognito2["default"])(this)._validations;
}
}, {
key: _symbols2["default"].attributes,
Expand All @@ -418,7 +404,7 @@ var Model = (function () {

var attributes = {};
this.properties.forEach(function (propertyName) {
if (!_this4._associations[propertyName]) {
if (!(0, _incognito2["default"])(_this4)._associations[propertyName]) {
attributes[propertyName] = _this4[propertyName];
}
});
Expand All @@ -433,8 +419,6 @@ var Model = (function () {
return true;
}
}
}, {
key: _symbols2["default"].callDeep,

/**
* Call a function deeply through all associations
Expand All @@ -444,6 +428,8 @@ var Model = (function () {
* @param {String} functionName The name of the function that you want to fire deeply.
* @param {function(errors, results)} Function called at the end of the operation.
*/
}, {
key: _symbols2["default"].callDeep,
value: function value(methodName, predicate, callback) {
var _this5 = this;

Expand Down Expand Up @@ -516,11 +502,13 @@ var Model = (function () {
}
});

var _ = (0, _incognito2["default"])(this);

//add belongsTo associations and remove others
Object.keys(this.associations).forEach(function (associationName) {
var relatedModel = _this6[associationName];
var foreignKeyField = (0, _jargon2["default"])(associationName).foreignKey.toString();
if (_this6._associations[associationName].type === "belongsTo") {
if (_._associations[associationName].type === "belongsTo") {
//try with relatedModel and relatedModel.id
if (relatedModel && relatedModel.id) {
fieldAttributes[foreignKeyField] = relatedModel.id;
Expand Down
Loading

0 comments on commit 98798ef

Please sign in to comment.