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() module.exports = function()
{ {
var def = {}, var type = typeof arguments[ 0 ]
name = '', result = null
type = typeof arguments[ 0 ]
; ;
switch ( type ) switch ( type )
{ {
// anonymous interface // anonymous interface
case 'object': case 'object':
def = arguments[ 0 ]; result = createAnonymousInterface.apply( null, arguments );
// 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."
);
}
break; break;
// named class // named class
case 'string': case 'string':
name = arguments[ 0 ]; result = createNamedInterface.apply( null, arguments );
def = arguments[ 1 ];
// add the name to the definition
def.__name = name;
break; break;
default: default:
@ -84,7 +66,7 @@ module.exports = function()
); );
} }
return extend( def ); return result;
}; };
@ -127,6 +109,47 @@ module.exports.isInterface = function( obj )
function Interface() {} 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 ) var extend = ( function( extending )
{ {
return function extend() return function extend()