About a year ago I wrote a post about how the Flex/As3 community needs a package manager like RubyGems for managing libraries and components.
Well, I have some good news. Recently I’ve learned about two potential solutions to this problem.
The first I learned about from a comment on the original post by Luke Bayes. Here is an excerpt:
I’ve been working on ’sprouts’ for the past year and finally landed on an architecture that actually sits on top of Rubygems for package management. Even though we have a functional pre-alpha in the wild right now, we expect to release a production-ready build before the end of January 2008 that will really support versioning via Rubygems the repository.
Check it out: http://www.projectsprouts.org
The second potential solution is a Flex plugin for Maven. It appears that it is still under development, but it looks promising. You can check it out here.
The as3 Inflector class can be used to pluralize or singularize most words. It is essentially a direct port of the Rails inflector class.
Here is a little demo Flex app that I put together to demo the classes functionality.

You can right click on the app to view the source and grab the class, or you can just click here.
A little while ago I posted about my ActionScript 3 implementation of the STOMP protocol. Well, I am just now getting around to posting that I created a google project site and released the source. This version is slightly updated from the one that was included in my example code in the previous post.
If you have any questions about this project, or if you would like to contribute, please let me know. Please post any bugs to this issue list.
I am pleased to announce the first release of as3yaml, an ActionScript 3 library for parsing and emitting YAML. It is a direct port of Ola Bini’s jvyaml, which was itself a port of Kirill Simonov’s PyYAML.
For those of you who are unfamiliar with YAML, here is a concise description from the yaml.org welcome page:
YAML(tm) (rhymes with “camel”) is a straightforward machine parsable data serialization format designed for human readability and interaction with scripting languages such as Perl and Python. YAML is optimized for data serialization, configuration settings, log files, Internet messaging and filtering. YAML(tm) is a balance of the following design goals:
- YAML documents are very readable by humans.
- YAML interacts well with scripting languages.
- YAML uses host languages’ native data structures.
- YAML has a consistent information model.
- YAML enables stream-based processing.
- YAML is expressive and extensible.
- YAML is easy to implement.
Here are some example of YAML taken from the current version of the YAML spec:
american:
- Boston Red Sox
- Detroit Tigers
- New York Yankees
national:
- New York Mets
- Chicago Cubs
- Atlanta Braves
-
name: Mark McGwire
hr: 65
avg: 0.278
-
name: Sammy Sosa
hr: 63
avg: 0.288
Here is an example of decoding a YAML string and converting it to ActionScript objects:
With the following YAML stored in a file called myYaml.yaml
---
Date: 2001-11-23 15:03:17 -5
User: ed
Fatal:
Unknown variable "bar"
Stack:
- file: TopClass.py
line: 23
code: |
x = MoreObject("345\n")
- file: MoreClass.py
line: 58
code: |-
foo = bar
You can load then load the YAML and decode it as follows.
public function loadYaml() : void
{
var loader : URLLoader = new URLLoader();
loader.load(new URLRequest('myYaml.yaml'));
loader.addEventListener(Event.COMPLETE, onYamlLoad);
}
public function onYamlLoad(event : Event) : void
{
var yamlMap : HashMap = YAML.decode(event.target.data) as HashMap; // returns a HashMap
trace(yamlMap.get("Date")); // returns a Date object and prints: Fri Nov 23 15:03:17 GMT-0500 2001
trace(yamlMap.get("User")); // returns a String and prints: ed
trace(yamlMap.get("Fatal")); // returns a String and prints: Unknown variable "bar"
trace(yamlMap.get("Stack")); // returns an Array and prints: [object HashMap],[object HashMap]
trace(yamlMap.get("Stack")[0].get("line")); // returns an Int and prints: 23
trace(yamlMap.get("Stack")[0].get("code")); // returns a String and prints: x = MoreObject("345\n")
}
There are some more examples available in the as3yaml docs.
I have been working on this for a little while now, and running it through various tests, so I think it is at a point where it is ready to be released into the wild. Please submit any bugs that you find to the issues list on the google project site. If you have any interest in contributing to the project, please let me know.
THANKS:
- Of course, special thanks to Ola Bini and Kirill Simonov for their excellent work on jvyaml and PyYaml, respectively.
- I would also like to thank the as3commons project. This library made the port from Java quite a bit easier than it might have been.
method_missing is one of the small bits of Ruby magic that can be used to some really amazing things when used properly, and some really dangerous things when used improperly. Rails dynamic finders are good example of the amazing things that you can do. Here are some others.
It is possible to implement something like method_missing in AS3 using the Proxy class. Here is a relatively simplistic example:
import flash.utils.Proxy;
import flash.utils.flash_proxy;
dynamic public class BaseProxy extends Proxy
{
flash_proxy override function callProperty(method: *, ...args): * {
try {
var clazz : Class = getDefinitionByName(getQualifiedClassName(this)) as Class;
return clazz.prototype[method].apply(method, args);
}
catch (e : Error) {
return methodMissing (method, args);
}
}
protected function methodMissing(method : *, args : Array) : Object{
throw( new Error("Method Missing"));
}
}
The callProperty method is called whenever an undefined method is called on an instance of this class, or any instance of class that extends this class. In the try block we check if the method was defined on the prototype. If it is not found there, we call methodMissing.
So, now if we create another class that extends this one like so
public dynamic class Model extends BaseProxy
{
public function myMethod (arg1 : String, arg2 : Boolean) : String {
return arg1 + " " + arg2;
}
}
and then run the following trace statements
import flash.display.Sprite;
public class MethodMissingExample extends Sprite
{
public function MethodMissingExample()
{
var m : Model = new Model();
Model.prototype.runtimeMethod = function (date : Date) : String {
return "I was defined at runtime at " + date.toLocaleTimeString();
};
trace(m.myMethod("I exist", true));
trace(m.runtimeMethod(new Date()));
trace(m.someMethod(0, false, "x"));
}
}
You will see the following in the console:
I exist true
I was defined at runtime at 09:58:00 PM
Error: Method Missing
The first call succeeds because myMethod is defined. someMethod is not defined, so a Method Missing error gets thrown.
Now, if we override the missingMethod in the Model class like so,
public dynamic class Model extends BaseProxy
{
public function myMethod (arg1 : String, arg2 : Boolean) : String {
return arg1 + " " + arg2;
}
override protected function methodMissing(method : *, args : Array) : Object {
return "You called " + method + " with " + args.toString();
}
}
and run the same trace statements you will see the following in the console:
I exist true
I was defined at runtime at 09:58:00 PM
You called someMethod with 0,false,x
In a previous post I discussed how you can use the [Mixin] metadata tag to create a static initialization in Flex. Here is another way to do pretty much the same thing:
public dynamic class Model
{
trace('I get called first');
public function Model()
{
}
}
The trace statement and any other code that is directly inside the class body will be invoked when the class is loaded. You can read more about this here.