<cfcomponent
output="false"
hint="This the brain of the shortie and is used to persist data between application instances.">
<cfset VARIABLES.Instance = StructNew() />
<cfset VARIABLES.Instance.DataFilePath = (
GetDirectoryFromPath( GetCurrentTemplatePath() ) &
"brain.xml"
) />
<cfset VARIABLES.Instance.Data = StructNew() />
<cffunction
name="Init"
access="public"
returntype="any"
output="false"
hint="Returns an initialized brain instance.">
<cfargument
name="DataFilePath"
type="string"
required="false"
default=""
/>
<cfif Len( ARGUMENTS.DataFilePath )>
<cfset VARIABLES.Instance.DataFilePath = ARGUMENTS.DataFilePath />
</cfif>
<cfset VARIABLES.LoadData() />
<cfreturn THIS />
</cffunction>
<cffunction
name="ClearData"
access="public"
returntype="void"
output="false"
hint="Clears the stored data.">
<cfset StructClear( VARIABLES.Instance.Data ) />
<cfset THIS.CommitData() />
<cfreturn />
</cffunction>
<cffunction
name="CommitData"
access="public"
returntype="void"
output="false"
hint="Commits the data to persited storage. This is a public access function so that the invoker can delay auto-committing during other methods.">
<cfset var LOCAL = StructNew() />
<cfwddx
action="CFML2WDDX"
input="#VARIABLES.Instance.Data#"
output="LOCAL.FileData"
/>
<cffile
action="WRITE"
file="#VARIABLES.Instance.DataFilePath#"
output="#LOCAL.FileData#"
/>
<cfreturn />
</cffunction>
<cffunction
name="Get"
acces="public"
returntype="any"
output="false"
hint="Gets a stored value at the given key. If no value is found, returns an empty string.">
<cfargument
name="Property"
type="string"
required="true"
/>
<cfif StructKeyExists(
VARIABLES.Instance.Data,
ARGUMENTS.Property
)>
<cfreturn VARIABLES.Instance.Data[ ARGUMENTS.Property ] />
<cfelse>
<cfreturn "" />
</cfif>
</cffunction>
<cffunction
name="LoadData"
access="private"
returntype="void"
output="false"
hint="Tries to load the XML file into the data struct. If the data file does not exist, no action is taken.">
<cfset var LOCAL = StructNew() />
<cfif FileExists( VARIABLES.Instance.DataFilePath )>
<cffile
action="READ"
file="#VARIABLES.Instance.DataFilePath#"
variable="LOCAL.FileData"
/>
<cfwddx
action="WDDX2CFML"
input="#LOCAL.FileData#"
output="VARIABLES.Instance.Data"
/>
</cfif>
<cfreturn />
</cffunction>
<cffunction
name="Set"
access="public"
returntype="void"
output="false"
hint="Sets the given property value at the given key.">
<cfargument
name="Property"
type="string"
required="true"
hint="The key used to access the property value."
/>
<cfargument
name="Value"
type="any"
required="true"
hint="The property value. This should be a simple value, but I am not enforcing that."
/>
<cfargument
name="CommitData"
type="boolean"
required="false"
default="true"
hint="If the invoker is planning on calling serveral sets in a row, they might choose to delay commiting and then commit manually after the last Set method call."
/>
<cfset VARIABLES.Instance.Data[ ARGUMENTS.Property ] = ARGUMENTS.Value />
<cfif ARGUMENTS.CommitData>
<cfset THIS.CommitData() />
</cfif>
<cfreturn />
</cffunction>
</cfcomponent>