1
0
Fork 0

Refactored interface module invocation into separate functions for named and anonymous

closure/master
Mike Gerwitz 2011-03-04 23:35:28 -05:00
parent ea5797ec4b
commit 649356ef23
1 changed files with 46 additions and 23 deletions

View File

@ -42,38 +42,20 @@ var util = require( './util' ),
*/
module.exports = function()
{
var def = {},
name = '',
type = typeof arguments[ 0 ]
var type = typeof arguments[ 0 ]
result = null
;
switch ( type )
{
// anonymous interface
case 'object':
def = arguments[ 0 ];
// ensure we have the proper number of arguments (if they passed in
// too many, it may signify that they don't know what they're doing,
// and likely they're not getting the result they're looking for)
if ( arguments.length > 1 )
{
throw Error(
"Expecting one argument for Interface definition; " +
arguments.length + " given."
);
}
result = createAnonymousInterface.apply( null, arguments );
break;
// named class
case 'string':
name = arguments[ 0 ];
def = arguments[ 1 ];
// add the name to the definition
def.__name = name;
result = createNamedInterface.apply( null, arguments );
break;
default:
@ -84,7 +66,7 @@ module.exports = function()
);
}
return extend( def );
return result;
};
@ -127,6 +109,47 @@ module.exports.isInterface = function( obj )
function Interface() {}
/**
* Creates a new anonymous Interface from the given interface definition
*
* @param {Object} def interface definition
*
* @return {Interface} new anonymous interface
*/
function createAnonymousInterface( def )
{
// ensure we have the proper number of arguments (if they passed in
// too many, it may signify that they don't know what they're doing,
// and likely they're not getting the result they're looking for)
if ( arguments.length > 1 )
{
throw Error(
"Expecting one argument for Interface definition; " +
arguments.length + " given."
);
}
return extend( def );
}
/**
* Creates a new named interface from the given interface definition
*
* @param {string} name interface name
* @param {Object} def interface definition
*
* @return {Interface} new named interface
*/
function createNamedInterface( name, def )
{
// add the name to the definition
def.__name = name;
return extend( def );
}
var extend = ( function( extending )
{
return function extend()