Skip to content

Instantly share code, notes, and snippets.

@chippinkston
Last active May 23, 2017 19:41
Show Gist options
  • Save chippinkston/deeb35526ab0a1aab155961115158329 to your computer and use it in GitHub Desktop.
Save chippinkston/deeb35526ab0a1aab155961115158329 to your computer and use it in GitHub Desktop.
Adam Tuttle's deORM
/*** Source: http://fusiongrokker.com/post/deorm ***/
public function deORM( obj = this, depth = 1, depthLimit = 10 ){
var deWormed = {};
if (depth >= depthLimit){
return {};
}
if (isSimpleValue( obj )){
deWormed = obj;
}
else if (isObject( obj )){
var md = getMetadata( obj );
do {
if (md.keyExists('properties')){
for (var prop in md.properties){
if (structKeyExists(obj, 'get' & prop.name)){
if ( !prop.keyExists('fieldtype') || prop.fieldtype == "id" ){
deWormed[ prop.name ] = invoke(obj, "get#prop.name#");
}
}
}
}
if (md.keyExists('extends')){
md = md.extends;
}
} while(md.keyExists('extends'));
}
else if (isStruct( obj )){
for (var key in obj){
deWormed[ key ] = deORM( obj[key], depth+1, depthLimit );
}
}
else if (isArray( obj )){
var deWormed = [];
for (var el in obj){
deWormed.append( deORM( el, depth+1, depthLimit ) );
}
}
else{
deWormed = getMetadata( obj );
}
return deWormed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment