Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "class" methods looking for .class set to true in method #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions lib/restful.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,45 @@ function _extend (router, resources, options, respond, routeInfo) {
cloned.parentID = exports.buildResourceId(routeInfo, arguments);
controller.create(req, res, resource, cloned, respond);
});
//
// If we are going to expose Class Resource methods to the router interface
//
if (options.exposeMethods) {
//
// Find every function on the resource,
// which has the "remote" property set to "true"
//
for (var m in resource) {
if(typeof resource[m] === "function" && resource[m].remote === true && resource[m].class === true) {
var self = this;

//
// For every function we intent to expose remotely,
// bind a GET and POST route to the method
//
(function(m){
self.path('/~' + m.toLowerCase(), function(){

var handler = function () {
var req = this.req,
res = this.res

preprocessRequest(req, resource, 'remote');
resource[m](req.restful.data, function(err, result){
req.restful.message = result;
return err
? respond(req, res, 500, err)
: respond(req, res, 200, 'result', result);
});
}

this.get(handler);
this.post(handler);
});
})(m)
}
}
}
//
// Bind /:resource/:param path
//
Expand Down Expand Up @@ -332,7 +370,7 @@ function _extend (router, resources, options, respond, routeInfo) {
// which has the "remote" property set to "true"
//
for (var m in resource) {
if(typeof resource[m] === "function" && resource[m].remote === true) {
if(typeof resource[m] === "function" && resource[m].remote === true && resource[m].class !== true) {
var self = this;

//
Expand All @@ -348,7 +386,7 @@ function _extend (router, resources, options, respond, routeInfo) {
_id = exports.buildResourceId(routeInfo, arguments);

preprocessRequest(req, resource, 'remote');
resource[m](_id, req.body, function(err, result){
resource[m](_id, req.restful.data, function(err, result){
req.restful.message = result;
req.restful.data.id = _id;
return err
Expand Down
20 changes: 19 additions & 1 deletion test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,25 @@ fixtures.Creature = resourceful.define('creature', function () {
}
this.hit.remote = true;


//
// This is a "class" functin,
// we will expose it as generic, and will not receive any id
//

this.ecosystem_life = function (options, callback) {
self.all(function(err, creatures){
if(err) {
return callback(err);
}
var life = 0;
for (var i=0; i<creatures.length; i++) life+=creatures[i].life;
callback(null, life);
});
}

this.ecosystem_life.remote = true;
this.ecosystem_life.class = true;

this._die = function (food) {
//
// Remark: We'll consider the _die function "private",
Expand Down
18 changes: 18 additions & 0 deletions test/macros/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ macros.resourceTest = function (name, options, context) {
var result = JSON.parse(body);
assert.equal(result.result, "I have been hit my life is: 10");
})
.next()
.get(prefix + '/creature/~ecosystem_life')
.expect(200)
.expect("should retrieve entire ecosystem life", function (err, res, body) {
var result = JSON.parse(body);
assert.isObject(result)
assert.isNumber(result.result)
assert.equal(result.result, "10");
})
.next()
.post(prefix + '/creature/~ecosystem_life')
.expect(200)
.expect("should retrieve entire ecosystem life", function (err, res, body) {
var result = JSON.parse(body);
assert.isObject(result)
assert.isNumber(result.result)
assert.equal(result.result, "10");
})
.next()
.get(prefix + '/creature/' + _id)
.expect(200)
Expand Down