Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Sep 05, 2012
@Marak Marak [tests] Added tests for Resource.method 2670180
@Marak Marak [fix] Curry no-option method calls. Return fn values on sync (no-call…
…back) method calls. Attach resource methods to Resource.methods object for convenience.
837c6ca
Showing with 185 additions and 4 deletions.
  1. +8 −4 lib/resourceful/resource.js
  2. +177 −0 test/resourceful-methods-test.js
View
12 lib/resourceful/resource.js
@@ -729,6 +729,9 @@ Resource.method = function (name, fn, schema) {
if (typeof schema === 'object') {
this[name] = function(){
var args = utile.args(arguments);
+ if(args.length === 0) {
+ args[0] = {};
+ }
var valid = validator.validate(args[0], schema);
if(!valid.valid) {
if(typeof args.cb === 'function') {
@@ -737,7 +740,7 @@ Resource.method = function (name, fn, schema) {
throw new Error(JSON.stringify(valid.errors)); // TODO: better errors
}
} else {
- fn.apply(this, arguments);
+ return fn.apply(this, arguments);
}
};
} else {
@@ -745,11 +748,13 @@ Resource.method = function (name, fn, schema) {
}
this[name].type = "method";
this[name].schema = schema;
+ if(typeof this.methods === "undefined") {
+ this.methods = {};
+ }
+ this.methods[name] = this[name];
return;
};
-
-
Resource.property = function (name, typeOrSchema, schema) {
var definer = {};
var type = (function () {
@@ -777,7 +782,6 @@ Resource.property = function (name, typeOrSchema, schema) {
this.schema.properties[name].conditions = {};
definer.name = name;
resourceful.mixin(definer, definers.all, definers[schema.type] || {});
-
return definer;
};
View
177 test/resourceful-methods-test.js
@@ -0,0 +1,177 @@
+var assert = require('assert'),
+ vows = require('vows'),
+ resourceful = require('../lib/resourceful');
+
+function describeResourceWithMethods(){
+ var r = resourceful.define('creature');
+
+ r.property('name');
+ r.property('type');
+
+ function poke(callback) {
+ if (callback) {
+ return callback(null, 'poked!');
+ }
+ return 'poked!';
+ }
+ function talk () {
+ console.log('talk');
+ };
+ function fire (options, callback) {
+ var result = {
+ status: "fired",
+ direction: options.direction,
+ power: options.power
+ };
+ if(callback) {
+ return callback(null, result);
+ }
+ return result;
+ };
+
+ r.method('poke', poke);
+
+ r.method('fire', fire, {
+ properties: {
+ "power": {
+ "type": "number",
+ "default": 1,
+ "required": true
+ },
+ "direction": {
+ "type": "string",
+ "enum": ["up", "down", "left", "right"],
+ "required": true,
+ "default": "up"
+ }
+ }});
+
+ r.method('talk', talk, { properties: {
+ "text": {
+ "type": "string",
+ "required": true
+ }
+ }});
+
+ return r;
+}
+
+vows.describe('resourceful').addVows({ // property
+ "A Resource with methods": {
+ topic: function () {
+ var r = describeResourceWithMethods();
+ return r;
+ },
+ "adds them to `Resource.methods`": function (R) {
+ assert.equal(Object.keys(R.methods).length, 3);
+ assert.include(R.methods, 'fire');
+ assert.include(R.methods, 'talk');
+ },
+ "should be able to fire simple method with callback" : function (R) {
+ R.poke(function(err, result){
+ assert.isNull(err);
+ assert.equal(result, 'poked!');
+ });
+ },
+ "should be able to fire simple method with no callback" : function (R) {
+ var result = R.poke();
+ assert.equal(result, 'poked!');
+ },
+ "should error on firing schema method with bad arguments and callback" : function (R) {
+ R.fire(function(err, result){
+ assert.isNotNull(err);
+ });
+ },
+ "should error on firing schema method with bad arguments and no callback" : function (R) {
+ try {
+ var result = R.fire();
+ } catch (err) {
+ assert.isNotNull(err);
+ return;
+ }
+ assert.equal(true, false)
+ },
+ "should be able to fire schema method with good arguments and callback" : function (R) {
+ R.fire({ power: 9001, direction: "up" }, function(err, result){
+ assert.isNull(err);
+ assert.isObject(result);
+ assert.equal(result.status, 'fired');
+ assert.equal(result.power, 9001);
+ assert.equal(result.direction, 'up');
+ });
+ },
+ "should be able to fire schema method with good arguments and no callback" : function (R) {
+ var result;
+ try {
+ result = R.fire({ power: 9001, direction: "up" });
+ } catch (err) {
+ assert.isNotNull(err);
+ return;
+ }
+ assert.isObject(result);
+ assert.equal(result.status, 'fired');
+ assert.equal(result.power, 9001);
+ assert.equal(result.direction, 'up');
+ },
+ "When instantiated": {
+ topic: function (R) {
+ var r = new(R)({ name: 'Larry', type: 'Dragon' });
+ return r;
+ },
+ "should respond to toJSON()": function (r) {
+ assert.isObject(r.toJSON());
+ },
+ "should return the attributes, when `Object.keys` is called": function (r) {
+ var keys = Object.keys(r);
+ assert.include(keys, 'fire');
+ assert.include(keys, 'talk');
+ },
+ "should be able to fire simple method with callback" : function (r) {
+ r.poke(function(err, result){
+ assert.isNull(err);
+ assert.equal(result, 'poked!');
+ });
+ },
+ "should be able to fire simple method with no callback" : function (r) {
+ var result = r.poke();
+ assert.equal(result, 'poked!');
+ },
+ "should error on firing schema method with bad arguments and callback" : function (r) {
+ r.fire(function(err, result){
+ assert.isNotNull(err);
+ });
+ },
+ "should error on firing schema method with bad arguments and no callback" : function (r) {
+ try {
+ var result = r.fire();
+ } catch (err) {
+ assert.isNotNull(err);
+ return;
+ }
+ assert.equal(true, false)
+ },
+ "should be able to fire schema method with good arguments and callback" : function (r) {
+ r.fire({ power: 9001, direction: "up" }, function(err, result){
+ assert.isNull(err);
+ assert.isObject(result);
+ assert.equal(result.status, 'fired');
+ assert.equal(result.power, 9001);
+ assert.equal(result.direction, 'up');
+ });
+ },
+ "should be able to fire schema method with good arguments and no callback" : function (r) {
+ var result;
+ try {
+ result = r.fire({ power: 9001, direction: "up" });
+ } catch (err) {
+ assert.isNull(err);
+ return;
+ }
+ assert.isObject(result);
+ assert.equal(result.status, 'fired');
+ assert.equal(result.power, 9001);
+ assert.equal(result.direction, 'up');
+ }
+ }
+ }
+}).export(module);

No commit comments for this range

Something went wrong with that request. Please try again.