I recently started a project of converting an older flex app to an AIR app. This app has to scan through a bunch of files, and classify certain types of media based on the location and file extension. Then it saves a manifest of the media to an xml file that can be loaded (without scanning every time).
I started out with a Kind class (think CollectionEventKind) that had the types as strings… but then I had to figure out how to store the regular expressions to make them easily accessible… Then I remembered that while everyone just uses Strings, static const vars can be any class, including it’s own class. So I came up with this class that you can use constants, or locate a specific constant. Code after the fold.
package aero.panasonic.training.model
{
import flash.utils.Dictionary;
import flash.utils.describeType;
public class MediaKind
{
public function MediaKind(title:String, label:String, regex:RegExp ){
this.title = title;
this.label = label;
this.regex = regex;
}
public function toString():String{
return title;
}
public var title:String;
public var label:String;
public var regex:RegExp;
public static const D_ILLUSTRATION_PERSPECTIVE:MediaKind =
new MediaKind("Illustration - Perspective", "Perspective", /Deliverables\/Illustrations\/Perspective\/.*_perspective\.(?:png|swf|jpg)/i );
public static const D_ILLUSTRATION_THREEQUARTER:MediaKind =
new MediaKind("Illustration - 3/4 (ISO)", "ISO", /Deliverables\/Illustrations\/Isometric\/.*_iso\.(?:png|swf|jpg)/i );
private static var lookup:Dictionary = new Dictionary();
private static var allKinds:Array = new Array();
private static var kindsMapped:Boolean = false;
private static function mapKinds():void{
var description:XML = describeType(MediaKind);
for each(var cXML:XML in description.constant){
var m:MediaKind = MediaKind[cXML.@name] as MediaKind;
lookup[m.title] = m;
lookup[m.label] = m;
allKinds.push(m);
}
kindsMapped = true;
}
public static function getMediaKind(input:String):MediaKind{
if(!kindsMapped) mapKinds();
return lookup[input];
}
public static function getAllKinds():Array{
if(!kindsMapped) mapKinds();
return allKinds;
}
}
}
In my code, I have a piece that gets all of the MediaKinds, then checks each file against each kind. I can filter, I can create drop-downs, all from one class. The getMediaKind() function lets me effortlessly create my ValueObjects from XML. The toString() writes out the title, making my XML human-readable.
Instinctively, I want to take this farther and create a base ‘Kind’ class that has this all in it. It turns out that static properties/functions aren’t inherited. The properties and functions are still in scope, however… so it may be possible to write a generic function that takes a class, creates a lookup for that specific class and passes it back down the line, allowing the allKinds() function to return ALL of the static definitions for the class and it’s super-classes. Anyways, that’s too much for my head at this point, but maybe someone else will take the bait