Refactored class module invocation method into separate functions
- The class module is getting too big. Something will have to be done soon.closure/master
parent
b158e542d5
commit
e3075b0479
103
lib/class.js
103
lib/class.js
|
@ -50,56 +50,20 @@ var class_meta = {};
|
||||||
*/
|
*/
|
||||||
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 class
|
// anonymous class
|
||||||
case 'object':
|
case 'object':
|
||||||
def = arguments[ 0 ];
|
result = createAnonymousClass.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 anonymous Class definition; " +
|
|
||||||
arguments.length + " given."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// named class
|
// named class
|
||||||
case 'string':
|
case 'string':
|
||||||
name = arguments[ 0 ];
|
result = createNamedClass.apply( null, arguments );
|
||||||
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"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
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() {};
|
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 );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue