Skip to content

Instantly share code, notes, and snippets.

@homestar9
Last active March 27, 2019 00:28
Show Gist options
  • Save homestar9/43563232d0c31eeb66813e31112767bc to your computer and use it in GitHub Desktop.
Save homestar9/43563232d0c31eeb66813e31112767bc to your computer and use it in GitHub Desktop.
Base Entity Class
component
hint="I am the base class for all entities"
{
property name="dateCreated" type="any" getter="true" setter="false"; // think of this like date first persisted
property name="dateModified" type="any" getter="true" setter="false"; // think of this like date last persisted
property name="createdBy" type="numeric" getter="true" setter="false"; // first persisted by
property name="modifiedBy" type="numeric" getter="true" setter="false"; // last persisted by
/*
UDFs
*/
include "/udf/hashify.cfm";
include "/udf/componentUtilities.cfm";
/*
CONSTRUCTOR (INIT)
*/
public function init(
any dateCreated = "",
any dateModified = "",
any createdBy = "",
any modifiedBy = ""
) {
// on init start
onInitStart(
argumentCollection = arguments
);
// set the default persistence variables
setDateCreated( arguments.dateCreated );
setDateModified( arguments.dateModified );
setCreatedBy( arguments.createdBy );
setModifiedBy( arguments.modifiedBy );
// on init
onInit(
argumentCollection = arguments
);
return this;
}
/*
ON BEHAVIOR HOOKS
Concrete implementations of entities can override these
*/
private function onInitStart() {};
private function onInit() {};
private function onCreatedStart( creator, dateCreated, id ) {}
private function onCreated( creator, dateCreated, id ) {}
private function onModifiedStart( modifier, dateModified ) {}
private function onModified( modifier, dateModified ) {}
/*
GETTERS
*/
function getCreator() {
return variables.creator;
}
function getDateCreated() {
return variables.dateCreated;
}
function getDateModified() {
return variables.dateCreated;
}
function getMemento() {
return variables;
}
function getModifier() {
return variables.modifier;
}
function getId() {
local.primaryKey = getPrimaryKey();
// if we don't have a primary key, throw an error
if ( !len( local.primaryKey ) ) {
throw( "I couldn't find the primary key!" );
}
return variables[ local.primaryKey ];
}
/*
GET PRIMARY KEY
Looks through the entity's metadata and looks for a primary key property value
*/
private string function getPrimaryKey() {
for ( var property in getComponentProperties() ) {
if (
structKeyExists( property, "primaryKey" ) &&
isBoolean( property.primaryKey ) &&
booleanFormat( property.primaryKey )
) {
return property.name;
}
}
return '';
}
/*
SETTERS (public)
*/
function setCreated(
required any id,
numeric createdBy = 0,
date dateCreated = now()
) {
// if we already have a date created, throw an error
if ( isDate( getDateCreated() ) ) {
throw( "entity has already been marked as created" );
}
// on start hook
onCreatedStart(
argumentCollection = arguments
);
setCreatedBy( arguments.createdBy );
setDateCreated( arguments.dateCreated );
// getPrimaryKey
local.primaryKey = getPrimaryKey();
if ( len( local.primaryKey ) ) {
variables[ local.primaryKey ] = arguments.id;
}
// on hook
onCreated(
argumentCollection = arguments
);
}
function setModified(
numeric modifiedBy = 0,
date dateModified = now()
) {
// on start hook
onModifiedStart(
argumentCollection = arguments
);
setModifiedBy( arguments.modifiedBy );
setDateModified( arguments.dateModified );
// on hook
onModified(
argumentCollection = arguments
);
}
/*
SETTERS (private)
*/
private function setCreatedBy( required any value ) {
if ( len(arguments.value) && !isValid( "integer", arguments.value ) ) {
throw( "invalid value for #getFunctionCalledName()#" );
}
variables.createdBy = arguments.value;
}
private function setDateCreated( required any value ) {
// will either be a valid date or empty string
variables.dateCreated = (isDate(arguments.value) ? arguments.value : "");
}
private function setDateModified( required any value ) {
// will either be a valid date or empty string
variables.dateModified = (isDate(arguments.value) ? arguments.value : "");
}
private function setModifiedBy( required any value ) {
if ( len(arguments.value) && !isValid( "integer", arguments.value ) ) {
throw( "invalid value for #getFunctionCalledName()#" );
}
variables.modifiedBy = arguments.value;
}
/**
* Populate from Properties
* runs a series of getters for each component property
* with a setter and a value in the passed structure
* @args (struct)
*/
private function populateFromProperties( required args ) {
// loop through this entity's properties
for ( var property in getComponentProperties() ) {
// do we have a setter for this?
// and do we have a corresponding key in the args
if ( (
structKeyExists( this, "set#property.name#") ||
structKeyExists( variables, "set#property.name#" )
) AND
structKeyExists( arguments.args, property.name )
) {
//writeDump( "set#property.name#( #arguments.args[ property.name ]# )" );
evaluate( "set#property.name#( arguments.args[ property.name ] )" );
}
}
}
/**
* Populate from Arguments
* runs setters (and optionally sets variables) for all arguments
* in the onInit() method
* @args (struct)
*/
private function populateFromArguments( required args ) {
// loop through this entity's properties
for ( var property in getComponentProperties() ) {
// do we have a setter for this?
// and do we have a corresponding key in the args
if (
(
structKeyExists( this, "set#property.name#") ||
structKeyExists( variables, "set#property.name#" )
) AND
structKeyExists( arguments.args, property.name )
) {
//writeDump( "set#property.name#( #arguments.args[ property.name ]# )" );
evaluate( "set#property.name#( arguments.args[ property.name ] )" );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment