ActiveRecord v0.2


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.utils.Proxy;
  4. import flash.utils.describeType;
  5. import flash.utils.getDefinitionByName;
  6. import flash.utils.getQualifiedClassName;
  7. import flash.utils.flash_proxy;
  8.  
  9. // import flash.utils.flash_proxy;
  10.  
  11. public dynamic class ActiveRecord extends Proxy
  12. {
  13. private static var functionsToAdd:Array = ["find_by_name","find_by_id","find_all"];
  14. protected static var methodFactory:DynamicMethodFactory;
  15. public function ActiveRecord()
  16. {
  17.  
  18. }
  19. public static function staticInitializer(klass:Class):void{
  20. var typeInfo:XML = describeType(klass);
  21. for each(var s:String in functionsToAdd){
  22. klass[s] = getMethod(typeInfo.@name,s);
  23. }
  24. }
  25. public static function getMethod(objectName:String,methodName:String):Function{
  26. return function(...args):Object{return endPoint(objectName,methodName,args);};
  27. }
  28. public static function endPoint(objectName:String,methodName:String,...args):Object{
  29. trace("You called: " + objectName + "." + methodName + "(" + args + ")");
  30. var klass:Class = getDefinitionByName(objectName) as Class;
  31. var o:ActiveRecord = new klass();
  32. return o;
  33. }
  34. flash_proxy override function callProperty(method: *, ...args): * {
  35. try {
  36. var clazz : Class = getDefinitionByName(getQualifiedClassName(this)) as Class;
  37. return clazz.prototype[method].apply(method, args);
  38. }
  39. catch (e : Error) {
  40. return methodMissing (method, args);
  41. }
  42. }
  43. protected function methodMissing(method : *, args : Array) : Object{
  44. throw( new Error("Method Missing"));
  45. return null;
  46. }
  47. }
  48. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.