flex - ActionScript: Package-level introspection? Or some other pluggable loading scheme? -


I'm writing some delicious business logic and I've come to a situation like this:

A set of rules ( ruleA , ruleB , etc ...), which all follow a standard pattern, I created a rule interface , And implemented it like rule A , RuleB , and so on.

So now I just need to load them.

I am currently using a map:

  var rule: object = {ruleA: new rule A (), ruleB: new rule B (),. ..}}  

Then calling it this way:

  function doStuff (which rule: string, some data: object): * {return rule [jorul ] .applyTo (some data); }  

Now, the rules follow a standard naming scheme, and they are all part of a package ( foo.rules ) ... There is

Thank you!

Edit : To clarify, find out the possible rule on the runtime, so I do not have to maintain a list of rules. For example:

  function loadRule (name: string): rule {import foo.rules; Var Rule: Class = foo.rules [name]; Return the new rule (); }  

, of course, I can not index in the foo.rules namespace ... but that's what I want to be able to do.

You can get reference to a class with a string with Flash. Retils.getDefinitionByName (className);

For example,

  def getRule (some value: string): class {return getDefinitionByName ("mybusiness.rules.Rule" + someValue); }  

In the above example, you are returning a class, but you can also install it and return the object.

However note that this will work only when the class is identified by the class name is compiled in your SDF if there is no one else in your code that you refer to that class, or It will not be compiled then it is usually obtained to create references in some other piece of code or your build system can pass the compiler.

Note, this task is the first class object in AS3, so depending on the case of your use, you can define each rule as the function of a rule class and pass it Can:

  def getRule (some value: string): function {new rule () ("theRuleName"); }  

It depends on the logic that you want to give to them, and how common is the code between these rules, of course.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -