Skip to content

Commit

Permalink
[test doc] Added documenatation for some methods and docs for JSON Sc…
Browse files Browse the repository at this point in the history
…hema. Updated tests
  • Loading branch information
indexzero committed Jul 30, 2010
1 parent 1994b96 commit 37d1663
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 64 deletions.
14 changes: 10 additions & 4 deletions lib/resourcer/core.js
Expand Up @@ -10,13 +10,11 @@ function pp(obj) {
}

resourcer.env = 'development';

resourcer.resources = {};
resourcer.Resource = require('resourcer/resource').Resource;
resourcer.engines = require('resourcer/engines');
resourcer.connection = new(resourcer.engines.memory.Connection);


//
// Select a storage engine
//
Expand All @@ -35,6 +33,7 @@ resourcer.use = function (engine) {
this.connect(null, {});
return this;
};

//
// Connect to the default storage engine
//
Expand All @@ -55,7 +54,7 @@ resourcer.connect = function (uri, port, options) {
};

//
// Factory
// Default Factory for creating new resources.
//
resourcer.defineResource = function (name, definition) {
if (typeof(name) === "function" && !definition) {
Expand Down Expand Up @@ -113,14 +112,21 @@ resourcer.defineResource = function (name, definition) {
F.init();

// Add this resource to the set of resources resourcer knows about
resourcer.resources[name] = F;
resourcer.register(name, F);

return F;
};

//
// Adds the Factory to the set of known resources
//
resourcer.register = function (name, Factory) {
return this.resources[name] = Factory;
};

//
// Removes the name from the set of known resources;
//
resourcer.unregister = function (name) {
delete this.resources[name];
};
Expand Down
44 changes: 37 additions & 7 deletions lib/resourcer/resource.js
Expand Up @@ -14,15 +14,27 @@ this.Resource.filter = require('resourcer/resource/view').filter;

this.Resource.views = {};

//
// Raises the init event. Called from resourcer.defineResource
//
this.Resource.init = function () {
this.emit('init', this);
};

//
// Registers the current instance's resource with resourcer
//
this.Resource.register = function () {
return resourcer.register(this.resource, this);
};

//
// Unregisters the current instance's resource from resourcer
//
this.Resource.unregister = function () {
return resourcer.unregister(this.resource);
};

this.Resource._request = function (/* method, [key, obj], callback */) {
var args = Array.prototype.slice.call(arguments),
that = this,
Expand Down Expand Up @@ -64,15 +76,19 @@ this.Resource._request = function (/* method, [key, obj], callback */) {
});
this.connection[method].apply(this.connection, args);
};

this.Resource.get = function (id, callback) {
return this._request("get", id, callback);
};

this.Resource.create = function (attrs, callback) {
return new(this)(attrs).save(callback);
};

this.Resource.save = function (obj, callback) {
return this._request("save", obj.key, obj, callback);
};

this.Resource.update = function (key, obj, callback) {
if (this.connection.protocol === 'database') {
return this._request("save", key, obj, callback);
Expand All @@ -81,39 +97,58 @@ this.Resource.update = function (key, obj, callback) {
return this._request("update", key, obj, callback);
}
};

this.Resource.all = function (callback) {
return this._request("all", callback);
};

this.Resource.view = function (path, params, callback) {
return this._request("view", path, params, callback);
};

this.Resource.find = function (conditions, callback) {
if (typeof(conditions) !== "object") {
throw new(TypeError)("`find` takes an object as first argument.");
}
return this._request("find", conditions, callback);
};

this.Resource.use = function () { return resourcer.use.apply(this, arguments) };
this.Resource.connect = function () { return resourcer.connect.apply(this, arguments) };

// Define getter / setter for connection property
this.Resource.__defineGetter__('connection', function () {
return this._connection || resourcer.connection;
});
this.Resource.__defineSetter__('connection', function (val) {
return this._connection = val;
});

// Define getter / setter for engine property
this.Resource.__defineGetter__('engine', function () {
return this._engine || resourcer.engine;
});
this.Resource.__defineSetter__('engine', function (val) {
return this._engine = val;
});
this.Resource.use = function () { return resourcer.use.apply(this, arguments) };
this.Resource.connect = function () { return resourcer.connect.apply(this, arguments) };

// Define getter / setter for resource property
this.Resource.__defineGetter__('resource', function () {
return this._resource;
});
this.Resource.__defineSetter__('resource', function (name) {
return this._resource = name;
});

// Define getter for properties, wraps this resources schema properties
this.Resource.__defineGetter__('properties', function () {
return this.schema.properties;
});

// Define getter / setter for key property. The key property is required by CouchDB
this.Resource.__defineSetter__('key', function (val) { return this._key = val });
this.Resource.__defineGetter__('key', function () { return this._key });

this.Resource.property = function (name, typeOrSchema, schema) {
var definer = {};
var type = (function () {
Expand All @@ -139,11 +174,6 @@ this.Resource.property = function (name, typeOrSchema, schema) {
this.Resource.define = function (schema) {
return resourcer.mixin(this.schema, schema);
};
this.Resource.__defineGetter__('properties', function () {
return this.schema.properties;
});
this.Resource.__defineSetter__('key', function (val) { return this._key = val });
this.Resource.__defineGetter__('key', function () { return this._key });

this.Resource.delegate = function (method, property) {
var that = this;
Expand Down
24 changes: 18 additions & 6 deletions lib/resourcer/schema.js
Expand Up @@ -23,6 +23,7 @@ this.definers = {
return this;
},
requires: function () {},
// JSON Schema 5.1: type
type: function (val) {
var valid = [
'string', 'number', 'integer',
Expand All @@ -36,6 +37,7 @@ this.definers = {
}
return this;
},
// JSON Schema 5.4: optional
optional: function (val, condition, options) {
enforceType(val, "boolean");
return this.define("optional", val, condition, options);
Expand All @@ -44,26 +46,29 @@ this.definers = {
enforceType(val, "boolean");
return this.define("unique", val, condition, options);
},
// JSON Schema 5.18: title
title: function (val) {
enforceType(val, "string");
this.property.title = val;

return this;
},
// JSON Schema 5.19: description
description: function (val) {
enforceType(val, "string");
this.property.description = val;

return this;
},
// JSON Schema 5.20: format
format: function (val, condition, options) {
var valid = [
'date', 'time', 'utc-millisec',
'regex', 'color', 'style',
'phone', 'uri', 'email',
'ip-address', 'ipv6', 'street-adress',
'country', 'region', 'postal-code',
'locality'
'date', 'time', 'utc-millisec',
'regex', 'color', 'style',
'phone', 'uri', 'email',
'ip-address', 'ipv6', 'street-adress',
'country', 'region', 'postal-code',
'locality', 'date-time',
];
if (valid.indexOf(val) !== -1) {
return this.define("format", val, condition, options);
Expand All @@ -87,33 +92,40 @@ this.definers = {
},
},
string: {
// JSON Schema 5.14: pattern
pattern: function (val, condition, options) {
enforceType(val, "regexp");
return this.define("pattern", val, condition, options);
},
// JSON Schema 5.16: minLength
minLength: function (val, condition, options) {
enforceType(val, "number");
return this.define("minLength", val, condition, options);
},
// JSON Schema 5.15: maxLength
maxLength: function (val, condition, options) {
enforceType(val, "number");
return this.define("maxLength", val, condition, options);
},
// Addition: Sets minLength and maxLength
length: function (val, condition, options) {
enforceType(val, "array");
return this.define("minLength", val[0], condition, options)
.define("maxLength", val[1], condition, options);
},
},
number: {
// JSON Schema 5.7: minimum
minimum: function (val, condition, options) {
enforceType(val, "number");
return this.define("minimum", val, condition, options);
},
// JSON Schema 5.8: maximum
maximum: function (val, condition, options) {
enforceType(val, "number");
return this.define("maximum", val, condition, options);
},
// Addition: Sets minimum and maximum
within: function (val, condition, options) {
enforceType(val, "array");
return this.define("minimum", val[0], condition, options)
Expand Down
43 changes: 0 additions & 43 deletions test/database-test.js
Expand Up @@ -43,7 +43,6 @@ vows.describe('resourcer/engines/database').addVows({
},
"a create() request": {
topic: function (r) {
this.r = r;
r.create({ _id: 'charlie', age: 30, hair: 'red'}, this.callback);
},
"should respond with a `201`": function (e, res) {
Expand All @@ -58,15 +57,6 @@ vows.describe('resourcer/engines/database').addVows({
assert.notEqual(obj._rev, undefined);
}
}
//"should create the record in the db": {
// topic: function (_, r) {
// r.get(99, this.callback);
// },
// "which can then be retrieved": function (e, res) {
// assert.isObject (resourcer.connection.store[99]);
// assert.equal (resourcer.connection.store[99].age, 30);
// }
//}
},
"a get() request": {
"when successful": {
Expand Down Expand Up @@ -116,39 +106,6 @@ vows.describe('resourcer/engines/database').addVows({
assert.equal(res.status, 201);
}
}
}
/*"a find() request": {
// Remark: Database engine currently doesn't support 'find', should we add it?
"when successful": {
topic: function (r) {
r.find({ hair: "black" }, this.callback);
},
"should respond with an array of length 2": function (e, obj) {
assert.length (obj, 2);
},
"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);
}
},
"when unsuccessful": {
topic: function (r) { r.find({ hair: "blue" }, this.callback); },
"should respond with an empty array": function (e, obj) {
assert.isArray (obj);
assert.length (obj, 0)
}
}
},
"an all() request": {
// Remark: Database engine currently doesn't support 'all', should we add it?
topic: function (r) {
r.all(this.callback);
},
"should respond with an array of all records": function (e, obj) {
assert.isArray (obj);
assert.length (obj, 3);
}
},*/
}
}).export(module);
8 changes: 4 additions & 4 deletions test/resource-view-test.js
Expand Up @@ -66,7 +66,7 @@ vows.describe('resourcer/resource/view').addVows({
this.Article = Article;
Article.published(this.callback);
},
"should return an array of all published Articles": function (res) {
"should return an array of all published Articles": function (e, res) {
var that = this;
assert.isArray (res);
assert.length (res, 3);
Expand All @@ -83,7 +83,7 @@ vows.describe('resourcer/resource/view').addVows({
topic: function (Article) {
Article.all(this.callback);
},
"should return an array of all Article records": function (res) {
"should return an array of all Article records": function (e, res) {
assert.isArray (res);
assert.length (res, 5);
}
Expand All @@ -92,7 +92,7 @@ vows.describe('resourcer/resource/view').addVows({
topic: function (Article) {
Article.by('cloudhead', this.callback);
},
"should return an array of Article records by 'cloudhead'": function (res) {
"should return an array of Article records by 'cloudhead'": function (e, res) {
assert.isArray (res);
assert.length (res, 3);
res.forEach(function (d) {
Expand All @@ -106,7 +106,7 @@ vows.describe('resourcer/resource/view').addVows({
topic: function (Article) {
Article.by('yoda', this.callback);
},
"should return an array of Article records by 'yoda'": function (res) {
"should return an array of Article records by 'yoda'": function (e, res) {
assert.isArray (res);
assert.length (res, 1);
assert.equal (res[0].author, 'yoda');
Expand Down

0 comments on commit 37d1663

Please sign in to comment.