Skip to content

Instantly share code, notes, and snippets.

@lptr
Created July 10, 2013 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lptr/5967261 to your computer and use it in GitHub Desktop.
Save lptr/5967261 to your computer and use it in GitHub Desktop.
package prezi.engine.doc;
import flash.geom.Matrix;
import hxjson2.JSON;
class Example
{
public static function main()
{
var object = document.getObject("obj-12");
var transform:Matrix = object.getProperty(ObjectProperties.transform);
// Type checking:
// Here setProperty can validate the type and find out that "lajos" is not a Matrix
object.setProperty(ObjectProperties.transform, "lajos");
}
public static function deserialize(propertyName:String, propertyValue:JSON):Dynamic
{
var valueType:Class = ObjectProperties.getDefinition(name).type;
return deserializers.get(valueType).deserialize(propertyValue);
}
}
// Type checking example
class ExampleModelObject
{
public function setProperty(property:PropertyDefinition, value:Dynamic)
{
if (!Std.is(value, property.type))
{
throw "Invalid property type";
}
// ...
}
}
class ObjectProperties
{
public static var transform = createDefinition("transform", Matrix, Single);
public static var children = createDefinition("children", ObjectReference, OrderedMulti);
public static var wrapWidth = createDefinition("wrapWidth", Float, OrderedMulti);
public static var text = createDefinition("text", Text, Single);
static var types:Hash<PropertyDefinition>;
static function createDefinition(name:String, resolution:Resolution, cardinality:Cardinality):PropertyDefinition
{
var type = new PropertyDefinition(name, resolution, cardinality);
types.set(name, type);
return type;
}
public static function getDefinition(name:String):PropertyDefinition
{
var definition = types.get(name);
if (definition == null)
{
// TODO: Is this going to cause problems?
// Like: user A knows this property, so they handle it as Text/OrderedList
// But user B doesn't, so they handle it as..?
definition = createDefinition(name, Unkonwn, Unkonwn);
}
return definition;
}
}
class PropertyDefinition
{
public var name(default, null):String;
public var type(default, null):Class;
public var cardinality(default, null):Cardinality;
public function new(name:String, type:Class, cardinality:Cardinality)
{
this.name = name;
this.type = type;
this.cardinality = cardinality;
}
}
enum Resolution
{
Value;
ObjectReference;
Text;
}
enum Cardinality
{
Single;
OrderedList;
SortedList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment