Skip to content

Instantly share code, notes, and snippets.

@jscheel
Created July 20, 2010 17:21
Show Gist options
  • Save jscheel/483247 to your computer and use it in GitHub Desktop.
Save jscheel/483247 to your computer and use it in GitHub Desktop.
package com.jaredscheel.utils
{
import flash.utils.Dictionary;
import flash.system.ApplicationDomain;
public class ClassManager
{
public static const instance:ClassManager = new ClassManager();
private var _classes:Dictionary = new Dictionary();
public function ClassManager()
{
if (instance)
{
throw new Error("Singleton Class -- use ClassManager.instance");
}
}
public function registerClassFromDomain(applicationDomain:ApplicationDomain, classPath:String):void
{
if (!_classes.hasOwnProperty(classPath))
{
var targetClass:Class = applicationDomain.getDefinition(classPath) as Class;
_classes[classPath] = targetClass;
}
}
public function get classes():Dictionary
{
return _classes;
}
//factory method, or you can do new ClassManager.instance.classes[classPath](...);
public function construct(classPath:String, args:Array = null):*
{
var proxy:Class = _classes[classPath];
if (args == null)
{
return new _classes[classPath]();
}
// This is absolutely horrible. Vote for args.unroll() or
// constructor-function.apply() compatibility here: https://bugs.adobe.com/jira/browse/FP-3364
switch(args.length)
{
case 0: return new _classes[classPath]();
case 1: return new _classes[classPath](args[0]);
case 2: return new _classes[classPath](args[0],args[1]);
case 3: return new _classes[classPath](args[0],args[1],args[2]);
case 4: return new _classes[classPath](args[0],args[1],args[2],args[3]);
case 5: return new _classes[classPath](args[0],args[1],args[2],args[3],args[4]);
case 6: return new _classes[classPath](args[0],args[1],args[2],args[3],args[4],args[5]);
case 7: return new _classes[classPath](args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
case 8: return new _classes[classPath](args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
case 9: return new _classes[classPath](args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
case 10: return new _classes[classPath](args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
default: throw new Error('Too many arguments, Vote for args.unroll() or constructor-function.apply() compatibility here: https://bugs.adobe.com/jira/browse/FP-3364');
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment