From e3075b0479541fc98252bc618f5bb76e78f323e2 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 4 Mar 2011 19:49:15 -0500 Subject: [PATCH] Refactored class module invocation method into separate functions - The class module is getting too big. Something will have to be done soon. --- lib/class.js | 103 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 41 deletions(-) diff --git a/lib/class.js b/lib/class.js index 332f6cd..3d9e0ef 100644 --- a/lib/class.js +++ b/lib/class.js @@ -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 ); +} /**