1
0
Fork 0

Refactored class module invocation method into separate functions

- The class module is getting too big. Something will have to be done soon.
closure/master
Mike Gerwitz 2011-03-04 19:49:15 -05:00
parent b158e542d5
commit e3075b0479
1 changed files with 62 additions and 41 deletions

View File

@ -50,56 +50,20 @@ var class_meta = {};
*/
module.exports = function()
{
var def = {},
name = '',
type = typeof arguments[ 0 ]
var type = typeof arguments[ 0 ],
result = null
;
switch ( type )
{
// anonymous class
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 anonymous Class definition; " +
arguments.length + " given."
);
}
result = createAnonymousClass.apply( null, arguments );
break;
// named class
case 'string':
name = arguments[ 0 ];
def = arguments[ 1 ];
// if too many arguments were provided, it's likely that they're
// expecting some result that they're not going to get
if ( arguments.length > 2 )
{
throw Error(
"Expecting two arguments for named Class definition; " +
arguments.length + " given."
);
}
// add the name to the definition
def.__name = name;
// the definition must be an object
if ( typeof def !== 'object' )
{
throw TypeError(
"Unexpected value for named class definition"
);
}
result = createNamedClass.apply( null, arguments );
break;
default:
@ -109,7 +73,7 @@ module.exports = function()
);
}
return extend( def );
return result;
};
@ -251,6 +215,63 @@ module.exports.isA = module.exports.isInstanceOf;
function Class() {};
/**
* Creates a new anonymous Class from the given class definition
*
* @param {Object} def class definition
*
* @return {Class} new anonymous class
*/
function createAnonymousClass( 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 anonymous Class definition; " +
arguments.length + " given."
);
}
return extend( def );
}
/**
* Creates a new named Class from the given class definition
*
* @param {string} name class name
* @param {Object} def class definition
*
* @return {Class} new named class
*/
function createNamedClass( name, def )
{
// if too many arguments were provided, it's likely that they're
// expecting some result that they're not going to get
if ( arguments.length > 2 )
{
throw Error(
"Expecting two arguments for named Class definition; " +
arguments.length + " given."
);
}
// add the name to the definition
def.__name = name;
// the definition must be an object
if ( typeof def !== 'object' )
{
throw TypeError(
"Unexpected value for named class definition"
);
}
return extend( def );
}
/**