Skip to content

Commit

Permalink
[merge] Merge from cloudhead upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Mar 30, 2011
2 parents 8a436f9 + cee8e1b commit b393540
Show file tree
Hide file tree
Showing 13 changed files with 513 additions and 192 deletions.
4 changes: 3 additions & 1 deletion lib/resourcer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ require.paths.unshift(__dirname, path.join(__dirname, 'vendor'));

var resourcer = exports;

resourcer.Resource = require('resourcer/resource').Resource;
resourcer.defineResource = require('resourcer/core').defineResource;
resourcer.defineProperty = require('resourcer/core').defineProperty;
resourcer.define = require('resourcer/core').define;
resourcer.use = require('resourcer/core').use;
resourcer.connect = require('resourcer/core').connect;
Expand All @@ -18,4 +20,4 @@ resourcer.unregister = require('resourcer/core').unregister;
resourcer.engine = require('resourcer/engines').memory;
resourcer.engines = require('resourcer/engines');

resourcer.resources.Resource = new(resourcer.defineResource);
resourcer.resources.Resource = resourcer.define('resource');
16 changes: 11 additions & 5 deletions lib/resourcer/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ this.Cache.prototype = {
this.store[id] = obj;
},
update: function (id, obj) {
for (var k in obj) {
try { this.store[id][k] = obj[k]; }
catch (ex) { }
if (id in this.store) {
for (var k in obj) {
this.store[id][k] = obj[k];
}
}
},
clear: function (id) {
if (id) { delete(this.store[id]) }
else { this.store = {} }
if (id) {
this.size --;
delete(this.store[id]);
} else {
this.size = 0;
this.store = {};
}
},
has: function (id) {
return id in this.store;
Expand Down
59 changes: 40 additions & 19 deletions lib/resourcer/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ resourcer.define = function (name, definition) {
name = definition.name;
}

name = capitalize(name || 'resource');
if (name) {
name = resourcer.capitalize(name);
} else { // Use the next available resource name
for (var i = 0; !name || (name in resourcer.resources); i ++) {
name = 'Resource' + i;
}
}

var F = function Resource(attrs) {
var that = this;
Expand All @@ -82,7 +88,7 @@ resourcer.define = function (name, definition) {
});

Object.keys(F.properties).forEach(function (k) {
that._properties[k] = undefined;
that._properties[k] = F.properties[k].default;
});

if (attrs) {
Expand All @@ -94,28 +100,18 @@ resourcer.define = function (name, definition) {
this._properties.resource = name;

Object.keys(this._properties).forEach(function (k) {
Object.defineProperty(that, k, {
get: function () {
return this.readProperty(k);
},
set: function (val) {
return this.writeProperty(k, val);
},
enumerable: true
});
resourcer.defineProperty(that, k);
});
};

// Setup inheritance
F.__proto__ = resourcer.Resource;
F.prototype.__proto__ = resourcer.Resource.prototype;

F.resource = name;
F.key = '_id';
F.emitter = new(events.EventEmitter);
F.emitter.addListener('error', function (e) {
// Logging
});
F.resource = name;
F.key = '_id';
F._children = [];
F._parents = [];

F.schema = {
name: name,
Expand All @@ -125,12 +121,25 @@ resourcer.define = function (name, definition) {
links: []
};

F.hooks = { before: {}, after: {} };

['get', 'save', 'update', 'create', 'destroy'].forEach(function (m) {
F.hooks.before[m] = [];
F.hooks.after[m] = [];
});

F.emitter = new(events.EventEmitter);

Object.keys(events.EventEmitter.prototype).forEach(function (k) {
F[k] = function () {
return F['emitter'][k].apply(F['emitter'], arguments);
};
});

F.on('error', function () {
// Logging
});

(definition || function () {}).call(F);

F.sync(function () {
Expand All @@ -144,6 +153,18 @@ resourcer.define = function (name, definition) {
};
resourcer.defineResource = resourcer.define;

resourcer.defineProperty = function (obj, property) {
Object.defineProperty(obj, property, {
get: function () {
return this.readProperty(property);
},
set: function (val) {
return this.writeProperty(property, val);
},
enumerable: true
});
};

//
// Adds the Factory to the set of known resources
//
Expand Down Expand Up @@ -196,6 +217,6 @@ resourcer.typeOf = function (value) {
//
// Utilities
//
function capitalize(str) {
resourcer.capitalize = function (str) {
return str && str[0].toUpperCase() + str.slice(1);
}
};
6 changes: 5 additions & 1 deletion lib/resourcer/engines/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ this.Connection.prototype = {
return this.put.apply(this, arguments);
},
update: function (id, doc, callback) {
this.request('merge', id, doc, callback);
if (this.cache.has(id)) {
this.put(id, resourcer.mixin({}, this.cache.get(id).toJSON(), doc), callback);
} else {
this.request('merge', id, doc, callback);
}
},
destroy: function () {
var that = this,
Expand Down
6 changes: 3 additions & 3 deletions lib/resourcer/engines/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ this.Connection.prototype = {
put: function () {
this.save.apply(this, arguments);
},
//update: function (key, obj, callback) {
// this.put(key, resourcer.mixin({}, this.store[key], obj), callback);
//},
update: function (key, obj, callback) {
this.put(key, resourcer.mixin({}, this.store[key], obj), callback);
},
get: function (key, callback) {
this.request(function () {
key = key.toString();
Expand Down
Loading

0 comments on commit b393540

Please sign in to comment.