Permalink
Browse files

Move hash functions to a separate file

  • Loading branch information...
1 parent 3e9573b commit a609ad9aee9209512380f03d30636a870da96464 @dresende committed Jan 3, 2012
Showing with 12 additions and 8 deletions.
  1. +8 −0 lib/hash.js
  2. +4 −8 lib/orm.js
View
@@ -0,0 +1,8 @@
+module.exports = {
+ hash: function (data) {
+ var h = require("crypto").createHash("md5");
+ h.update(JSON.stringify(data));
+
+ return h.digest("hex");
+ }
+}
View
@@ -104,20 +104,19 @@ ORM.prototype.define = function (model, fields, colParams) {
}
}
- var h = crypto.createHash("md5");
- h.update(JSON.stringify(data));
-
- this._dataHash = h.digest("hex");
+ this._dataHash = require("./hash").hash(data);
if (this._dataPending == 0) {
this.emit("ready", this);
}
};
+ // this adds events to instances
util.inherits(Model, events.EventEmitter);
associationHelpers.one = require("./associations/one").define(orm, Model, model, fields, colParams);
associationHelpers.many = require("./associations/many").define(orm, Model, model, fields, colParams);
+ // this adds events to object
require("./events").extend(Model);
Model.prototype._getData = function () {
@@ -180,10 +179,7 @@ ORM.prototype.define = function (model, fields, colParams) {
return (this._pending == 0);
};
Model.prototype.saved = function () {
- var h = crypto.createHash("md5");
- h.update(JSON.stringify(this._getData()));
-
- return (this._dataHash == h.digest("hex"));
+ return (this._dataHash == require("./hash").hash(this._getData()));
};
Model.prototype.created = function () {
return this.hasOwnProperty("id");

0 comments on commit a609ad9

Please sign in to comment.