Permalink
Browse files

[examples] Cleaning up resourceful method examples.

  • Loading branch information...
1 parent 67f63f9 commit 77a03026a50527f8fbf2727868bc4a67d75ca57a @Marak Marak committed Sep 7, 2012
Showing with 104 additions and 60 deletions.
  1. +0 −60 examples/custom-methods.js
  2. +104 −0 examples/resourceful-methods.js
View
@@ -1,60 +0,0 @@
-//
-// custom-methods.js - example of extending resources to have methods
-//
-
-var resourceful = require('../lib/resourceful');
-resourceful.use('memory');
-
-var Creature = resourceful.define('creature');
-
-var lazer = function(options){
- console.log('firing mah lazar @ ' + options.powa);
-};
-
-//
-// Resource.method takes three arguments:
-//
-// name, Function, schema
-//
-// The schema represents an optional JSON-Schema,
-// which if present, is enforced on the Function's arguments
-//
-//
-Creature.method('fire', lazer, { properties: {
- "powa": {
- "type": "string",
- "enum": ["high", "low", "med", "SUPA POWA"],
- "required": true
- }
-}});
-
-Creature.fire({ powa: "med"});
-Creature.fire({ powa: "TOO HIGH"}, function(err, result){
- // errors since TOO HIGH is not in enum
- console.log(err, result);
-});
-
-// throws validation error
-// Creature.fire({ powa: "TOO HIGH"});
-
-Creature.create({
- id: 'Marak'
-}, function(err, marak){
- if(err){
- console.log(err.validate)
-
- }
- marak.fire({ powa: "med"});
-
- // throws validation error
- // marak.fire({ powa: "TOO HIGH"});
-
- marak.fire({ powa: "low"}, function(err, result){
- console.log(err, result);
- });
-
- marak.fire({ powa: "TOO HIGH"}, function(err, result){
- // errors since TOO HIGH is not in enum
- console.log(err, result);
- });
-});
@@ -0,0 +1,104 @@
+//
+// custom-methods.js - example of extending resources to have methods
+//
+
+var resourceful = require('../lib/resourceful');
+resourceful.use('memory');
+
+var Creature = resourceful.define('creature');
+
+function fire (options, callback) {
+ var result = {
+ status: "fired",
+ direction: options.direction,
+ power: options.power
+ };
+ if(callback) {
+ return callback(null, result);
+ }
+ return result;
+};
+//
+// Resource.method takes three arguments:
+//
+// name, Function, schema
+//
+// The schema represents an optional JSON-Schema,
+// which if present, is enforced on the Function's arguments
+//
+//
+
+var poke = function(target, velocity){
+ console.log(target, velocity);
+};
+
+Creature.method('poke', poke, { properties: {
+ "target": {
+ "type": "string",
+ "required": true
+ },
+ "velocity": {
+ "type": "string",
+ "default": "ten"
+ }
+}});
+
+
+Creature.method('fire', fire, {
+ "description": "fires a lazer at a certain power and direction",
+ "properties": {
+ "options": {
+ "type": "object",
+ "properties": {
+ "power": {
+ "type": "number",
+ "default": 1
+ },
+ "direction": {
+ "type": "string",
+ "enum": ["up", "down", "left", "right"],
+ "required": true,
+ "default": "up"
+ }
+ }
+ },
+ "callback": {
+ "type": "function",
+ "required": false
+ }
+ }
+});
+
+Creature.fire({ direction: "side-ways"}, function(err, result){
+ // errors since side-ways is not in enum
+ console.log(err, result);
+});
+Creature.poke("someone");
+Creature.poke("someone-else", "six");
+
+// throws validation error
+// Creature.fire({ powa: "TOO HIGH"});
+
+Creature.create({
+ id: 'Marak'
+}, function(err, marak){
+ if(err){
+ console.log(err);
+ }
+
+ marak.poke("somebody");
+ marak.poke("somebody-else", "seven");
+
+ marak.fire({ "direction": "side-ways", "power": 20}, function(err, result){
+ // errors since Tside-ways is not in enum
+ console.log(err, result);
+ });
+
+ // throws validation error
+ // marak.fire({ power: "TOO HIGH"});
+ marak.fire({ "direction": "up", "power": 20}, function(err, result){
+ // fires correctly
+ console.log(err, result);
+ });
+});
+

0 comments on commit 77a0302

Please sign in to comment.