Created
April 26, 2017 21:23
-
-
Save jclausen/5dc195c9c854fa8bdb171788e8567c13 to your computer and use it in GitHub Desktop.
Memento
This file contains hidden or 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
/** | |
* Convenience method to retreive the default memento and rename the primary key in the struct, if necessary | |
* | |
* @renameKey boolean Whether or not to rename the primary key column of the structure to reflect the entity name (important for auto-population of new models) | |
* @recurse boolean Whether to recurse in to the `to-many` arrays and provide the PKs of those relationships | |
**/ | |
public function asStruct( renameKey=false, recurse=false ){ | |
var s = getMemento( recurse=arguments.recurse ); | |
if( | |
arguments.renamekey | |
&& | |
structKeyExists( s, this.getKey() ) | |
){ | |
s[ lcase( getEntityGivenName() ) & "_id" ] = s[ this.getKey() ]; | |
structDelete( s, this.getKey() ); | |
} | |
return s; | |
} | |
/** | |
* The default memento function for all objects - returns a shallow struct of the active entity | |
* @exclude array An array of properties to exclude | |
* @recurse boolean Whether to recurse in to the `to-many` arrays and provide the PKs of those relationships | |
**/ | |
public function getMemento( exclude=[], recurse=false ){ | |
var properties = getIdentifierColumnNames(); | |
var inherentProperties = getPropertyNames( getEntityGivenName( this ) ); | |
//merge our latter in to the former because of the javatype returned by getPropertyNames() | |
arrayAppend( properties, inherentProperties, true ); | |
return getBaseMemento( properties=properties, exclude=arguments.exclude, recurse=arguments.recurse ); | |
} | |
/** | |
* Build out property mementos from simple properties only. | |
* @properties array The properties array to build the memento | |
* @excludes array The properties to exclude from the memento | |
* @recurse boolean Whether to recurse in to the `to-many` arrays and provide the PKs of those relationships | |
*/ | |
public struct function getBaseMemento( | |
required array properties, | |
excludes=[], | |
recurse=false | |
){ | |
var result = {}; | |
if( !isArray( arguments.excludes ) ){ | |
arguments.excludes = listToArray( arguments.list ); | |
} | |
// base property additions | |
var propertyAdditions = [ 'isActive', 'createdTime', 'modifiedTime' ]; | |
for( var addProp in propertyAdditions ){ | |
if( | |
!arrayFindNoCase( arguments.properties, addProp ) | |
&& | |
hasOwnProperty( addProp ) | |
) arguments.properties.append( addProp ); | |
} | |
// properties | |
for( var thisProp in arguments.properties ){ | |
if( structKeyExists( variables, thisProp ) && !arrayFindNoCase( arguments.excludes, thisProp ) ){ | |
if( isDate( variables[ thisProp ] ) ){ | |
try{ | |
} catch( any e ){ | |
result[ thisProp ] = variables[ thisProp ]; | |
} | |
} else if( isSimpleValue( variables[ thisProp ] ) ){ | |
//auto-unpack any json fields | |
if( | |
isSimpleValue( variables[ thisProp ] ) | |
&& | |
( | |
left( trim( variables[ thisProp ] ), 1 ) == '[' | |
|| | |
left( trim( variables[ thisProp ] ), 1 ) == '{' | |
) | |
&& | |
isJSON( variables[ thisProp ] ) ) | |
{ | |
result[ thisProp ] = deSerializeJSON( variables[ thisProp ] ); | |
} else { | |
result[ thisProp ] = variables[ thisProp ]; | |
} | |
//single entity relationships we retrieve the primary key | |
} else if( isObject( variables[ thisProp ] ) ) { | |
result[ thisProp ] = variables[ thisProp ].getId(); | |
} else if( arguments.recurse && !isNull( variables[ thisProp ] ) && isArray( variables[ thisProp ] ) ){ | |
} else if( !isNull( variables[ thisProp ] ) && isArray( variables[ thisProp ] ) ) { | |
result[ thisProp ] = []; | |
for( var rel in variables[ thisProp ] ){ | |
arrayAppend( result[ thisProp ], rel.getId() ); | |
} | |
//add a placeholder for any unaccounted for items to avoid any further recursion | |
} else { | |
result[ thisProp ] = ""; | |
}//end if/else on typing | |
} else if( !arrayFindNoCase( arguments.excludes, thisProp ) ){ | |
result[ thisProp ] = ""; | |
}//end if/else on struct key exists | |
}//end for property | |
return result; | |
}//end getBaseMemento | |
/** | |
* returns an array of properties that make up the identifier - convenience facade for `getKey()` to ensure consistent data type | |
* @output false | |
**/ | |
public array function getIdentifierColumnNames(){ | |
var identifiers = getKey( getEntityGivenName( this ) ); | |
if( isSimpleValue( identifiers ) ){ | |
identifiers = [ identifiers ]; | |
} | |
return identifiers; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment