Skip to content

Commit

Permalink
moved method shortcuts ('GET', etc) to own files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jed committed Apr 25, 2010
1 parent 64e6b40 commit 64bd517
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
5 changes: 5 additions & 0 deletions apps/fab.method.DELETE.js
@@ -0,0 +1,5 @@
var fab = { method: require( "./fab.method" ).app };

exports.summary = "Shortcut for fab.method( 'DELETE' ).";

exports.app = fab.method( "DELETE" )
5 changes: 5 additions & 0 deletions apps/fab.method.GET.js
@@ -0,0 +1,5 @@
var fab = { method: require( "./fab.method" ).app };

exports.summary = "Shortcut for fab.method( 'GET' ).";

exports.app = fab.method( "GET" )
5 changes: 5 additions & 0 deletions apps/fab.method.POST.js
@@ -0,0 +1,5 @@
var fab = { method: require( "./fab.method" ).app };

exports.summary = "Shortcut for fab.method( 'POST' ).";

exports.app = fab.method( "POST" )
5 changes: 5 additions & 0 deletions apps/fab.method.PUT.js
@@ -0,0 +1,5 @@
var fab = { method: require( "./fab.method" ).app };

exports.summary = "Shortcut for fab.method( 'PUT' ).";

exports.app = fab.method( "PUT" )
46 changes: 20 additions & 26 deletions apps/fab.method.js
@@ -1,36 +1,30 @@
exports.summary = "Takes one more more method names, and returns a ternary app that passes the request to the first app when the request method matches, and to the second app otherwise.";

method = exports.app = ( function( names ) {
for ( var name; name = names.pop(); ) method[ name ] = method( name );

return method;

function method() {
var
methods = {},
len = arguments.length;
method = exports.app = function( names ) {
var
methods = {},
len = arguments.length;

for ( var i = 0; i < len; i++ ) {
methods[ arguments[ i ] ] = true;
}

for ( var i = 0; i < len; i++ ) {
methods[ arguments[ i ] ] = true;
}

return function( hit ) {
return function( miss ) {
return function() {
var out = this;
return function( head ) {
var app = head.method in methods ? hit : miss;

app = app.call( out );
if ( app ) app = app( head );

return app;
}
return function( hit ) {
return function( miss ) {
return function() {
var out = this;
return function( head ) {
var app = head.method in methods ? hit : miss;

app = app.call( out );
if ( app ) app = app( head );

return app;
}
}
}
}
})( [ "GET", "POST", "PUT", "DELETE" ] );
}

exports.tests = ( function() {
var ok = function(){ this({ body: true }) }
Expand Down

0 comments on commit 64bd517

Please sign in to comment.