Created
July 10, 2013 15:27
-
-
Save lptr/5967261 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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