Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[test] Tested abstract auto id gen

  • Loading branch information...
commit 741fc08bf882bc711318ec8fce8ac30e1dfef4ed 1 parent 1ca70f6
@pksunkara pksunkara authored
View
2  README.md
@@ -71,7 +71,7 @@ Now lets instantiate a Creature, and feed it:
``` js
var wolf = new(Creature)({
- diet: 'carnivor',
+ diet: 'carnivore',
vertebrate: true
});
View
2  examples/creature-function.js
@@ -11,7 +11,7 @@ var Creature = resourceful.define('creature', function () {
});
var wolf = new(Creature)({
- diet: 'carnivor',
+ diet: 'carnivore',
vertebrate: true
});
View
2  examples/creature-inline.js
@@ -11,7 +11,7 @@ Creature.prototype.feed = function (food) {
};
var wolf = new(Creature)({
- diet: 'carnivor',
+ diet: 'carnivore',
vertebrate: true
});
View
6 lib/resourceful/engines/couchdb/index.js
@@ -65,16 +65,16 @@ Couchdb.prototype.put = function (id, doc, callback) {
if (e) return callback(e);
res.status = 201;
- callback(null, resourceful.mixin(doc, res));
+ callback(null, resourceful.mixin({}, doc, res));
});
};
Couchdb.prototype.post = Couchdb.prototype.create = function (doc, callback) {
return this.request('post', doc, function (e, res) {
if (e) return callback(e);
-
+
res.status = 201;
- callback(null, res);
+ callback(null, resourceful.mixin({}, doc, res));
});
};
View
8 lib/resourceful/engines/memory.js
@@ -10,9 +10,8 @@ var Memory = exports.Memory = function (options) {
options = options || {};
this.uri = options.uri;
- this.getNextID = function getNextID () {
- counter ++;
- return counter;
+ this.increment = function () {
+ return ++counter;
}
if (typeof(this.uri) === 'string') {
@@ -66,7 +65,8 @@ Memory.prototype.save = function (key, val, callback) {
var args = Array.prototype.slice.call(arguments);
var callback = args.pop(), val = args.pop();
if (!args.length || !key) {
- key = this.getNextID();
+ key = this.increment();
+ val._id = key;
}
this.request(function () {
View
7 lib/resourceful/resource.js
@@ -141,7 +141,12 @@ Resource._request = function (/* method, [key, obj], callback */) {
}
obj._rev = result.rev;
}
- result = resourceful.instantiate.call(that, method === 'get' || method === 'update' ? result : obj);
+
+ if (method === 'save') {
+ obj._id = result._id || result.id;
+ }
+
+ result = resourceful.instantiate.call(that, ['get', 'update'].indexOf(method) !== -1 ? result : obj);
if (method === 'update') {
// TODO: Not necessary for identity map
View
13 test/engines-test.js
@@ -286,5 +286,18 @@ engines.forEach(function (e) {
}
}
}
+ }).addBatch({
+ "Creating object without 'id'": {
+ topic: function () {
+ resources[e].Author.create({ age: 51, hair: 'white' }, this.callback);
+ },
+ "should be successful": function (err, obj) {
+ assert.isNull(err);
+ assert.notEqual(obj._id, undefined);
+ assert.equal(obj.age, 51);
+ assert.equal(obj.hair, 'white');
+ assert.equal(obj.resource, 'Author');
+ }
+ }
}).export(module)
});
Please sign in to comment.
Something went wrong with that request. Please try again.