Permalink
Browse files

change the way resource.define works

When no resource name is supplied, we generate
an available resource name. So eaach call of resourcer.define
returns a new Constructor, instead of the default one every time.
  • Loading branch information...
1 parent d9d89a7 commit 4077756cf4a6a495e66980259e2c62fedbadfa95 @cloudhead committed Feb 28, 2011
Showing with 35 additions and 20 deletions.
  1. +3 −1 lib/resourcer.js
  2. +7 −1 lib/resourcer/core.js
  3. +9 −4 test/database-test.js
  4. +16 −14 test/resourcer-test.js
View
4 lib/resourcer.js
@@ -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;
@@ -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');
View
8 lib/resourcer/core.js
@@ -70,7 +70,13 @@ resourcer.define = function (name, definition) {
name = definition.name;
}
- name = resourcer.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;
View
13 test/database-test.js
@@ -13,6 +13,8 @@ var cradle = require('cradle');
var resourcer = require('resourcer');
+resourcer.env = 'test';
+
vows.describe('resourcer/engines/database').addVows({
"A database containing default resources": {
topic: function () {
@@ -24,19 +26,22 @@ vows.describe('resourcer/engines/database').addVows({
{ _id: 'bob', age: 35, hair: 'black'},
{ _id: 'tim', age: 16, hair: 'brown'},
{ _id: 'mat', age: 29, hair: 'black'}
- ], function () {
- promise.emit('success');
+ ], function (e, res) {
+ promise.emit('success', res);
});
});
})
return promise;
},
- "is created": function () {}
+ "is created": function (e, obj) {
+ assert.isNull (e);
+ assert.isArray(obj);
+ }
}
}).addVows({
"A default Resource factory" : {
topic: function() {
- return this.Factory = resourcer.defineResource(function () {
+ return this.Factory = resourcer.define('user', function () {
this.use('database');
});
},
View
30 test/resourcer-test.js
@@ -81,15 +81,15 @@ vows.describe('resourcer').addVows({
}).addVows({ // API
"Default Resource instances": {
topic: function () {
- return resourcer.defineResource();
+ return resourcer.define();
},
"have the `resource`, `property` and `define` methods": function (r) {
assert.isString (r.resource);
assert.isFunction (r.property);
assert.isFunction (r.define);
},
"resource should be set to 'Resource'": function (r) {
- assert.equal (r.resource, 'Resource');
+ assert.match (r.resource, /^Resource\d+/);
},
"the `properties` accessor returns an object with only the '_id' property": function (r) {
assert.isObject (r.properties);
@@ -104,7 +104,7 @@ vows.describe('resourcer').addVows({
}).addVows({ // property
"A Resource with a couple of properties": {
topic: function () {
- var r = resourcer.defineResource();
+ var r = resourcer.define('book');
r.property('title');
r.property('kind');
return r;
@@ -119,7 +119,7 @@ vows.describe('resourcer').addVows({
return new(R)({ title: 'The Great Gatsby' });
},
"should respond to toString()": function (r) {
- assert.equal (r.toString(), '{"title":"The Great Gatsby","resource":"Resource"}');
+ assert.equal (r.toString(), '{"title":"The Great Gatsby","resource":"Book"}');
},
"should respond to toJSON()": function (r) {
assert.isObject (r.toJSON());
@@ -295,18 +295,18 @@ vows.describe('resourcer').addVows({
tim: { _id: 43, age: 16, hair: 'brown'},
mat: { _id: 44, age: 29, hair: 'black'}
});
- return resourcer.defineResource();
+ return resourcer.defineResource('poop');
},
"a get() request": {
"when successful": {
topic: function (r) {
+ this.Factory = r;
r.get("bob", this.callback);
},
"should respond with a Resource instance": function (e, obj) {
-
assert.isObject (obj);
- assert.instanceOf (obj, resourcer.resources.Resource);
- assert.equal (obj.constructor, resourcer.resources.Resource);
+ assert.instanceOf (obj, resourcer.Resource);
+ assert.equal (obj.constructor, this.Factory);
},
"should respond with the right object": function (e, obj) {
assert.equal (obj._id, 42);
@@ -332,8 +332,8 @@ vows.describe('resourcer').addVows({
},
"should respond with an array of Resource instances": function (e, obj) {
assert.isArray (obj);
- assert.instanceOf (obj[0], resourcer.resources.Resource);
- assert.instanceOf (obj[1], resourcer.resources.Resource);
+ assert.instanceOf (obj[0], resourcer.Resource);
+ assert.instanceOf (obj[1], resourcer.Resource);
}
},
"when unsuccessful": {
@@ -359,6 +359,7 @@ vows.describe('resourcer').addVows({
r.create({ _id: 99, age: 30, hair: 'red'}, this.callback);
},
"should return the newly created object": function (e, obj) {
+ assert.strictEqual(obj.constructor, this.Factory);
assert.instanceOf(obj, this.Factory);
assert.equal(obj.id, 99);
},
@@ -406,13 +407,14 @@ vows.describe('resourcer').addVows({
},
"an all() request": {
topic: function (r) {
+ this.Factory = r;
r.all(this.callback);
},
"should respond with a mix of Resource and Article instances": function (e, obj) {
assert.equal (obj[0].constructor, resourcer.resources.Article);
assert.equal (obj[1].constructor, resourcer.resources.Article);
- assert.equal (obj[2].constructor, resourcer.resources.Resource);
- assert.equal (obj[3].constructor, resourcer.resources.Resource);
+ assert.equal (obj[2].constructor, this.Factory);
+ assert.equal (obj[3].constructor, this.Factory);
}
}
},
@@ -439,7 +441,7 @@ vows.describe('resourcer').addVows({
assert.equal (this.connection.store[42].name, "bob");
},
"should set the `resource` attribute accordingly": function (res) {
- assert.equal (this.connection.store[42].resource, "Resource");
+ assert.equal (this.connection.store[42].resource, this.Resource._resource);
},
"should set the `isNewRecord` flag to false": function () {
assert.strictEqual (this.r.isNewRecord, false);
@@ -457,7 +459,7 @@ vows.describe('resourcer').addVows({
"with a user resource": {
topic: function () {
var conn = this.connection = new(resourcer.engines.memory.Connection)();
- this.User = resourcer.defineResource('User', function () {
+ this.User = resourcer.define('user', function () {
this.connection = conn;
});
return new(this.User)({ _id: 55, name: "fab" });

0 comments on commit 4077756

Please sign in to comment.