My Shortie: Ray Camden's Beginner ColdFusion Contest (Monster Maker)

<cfcomponent
	output="false"
	hint="This the brain of the shortie and is used to persist data between application instances.">

	<!---
		Run pseudo constructor. Here is where we can set
		up default data structures and data values.
	--->

	<!---
		Create a private data structure to hold all instance
		related data.
	--->
	<cfset VARIABLES.Instance = StructNew() />

	<!---
		We are going to be persisting this data via an XML
		file that we write to disk. This is the file path
		of the XML file. By default, we are going to store
		the XML file in the same directory (but this can be
		overridden in the INIT method).
	--->
	<cfset VARIABLES.Instance.DataFilePath = (
		GetDirectoryFromPath( GetCurrentTemplatePath() ) &
		"brain.xml"
		) />

	<!---
		This is the struct in which we are going to hold the
		data. This is going to be the only form of data we
		store. I am going to assume that this struct will
		only hold simple value. I am not going to enforce this.
	--->
	<cfset VARIABLES.Instance.Data = StructNew() />



	<cffunction
		name="Init"
		access="public"
		returntype="any"
		output="false"
		hint="Returns an initialized brain instance.">

		<!--- Define arguments. --->
		<cfargument
			name="DataFilePath"
			type="string"
			required="false"
			default=""
			/>


		<!---
			Check to see if we were passed a new file path.
			If so, then store this one as our XML file path.
		--->
		<cfif Len( ARGUMENTS.DataFilePath )>

			<cfset VARIABLES.Instance.DataFilePath = ARGUMENTS.DataFilePath />

		</cfif>


		<!---
			Now that we have a data file stored, we can read
			in what ever data might already exists. Load the
			persisted data.
		--->
		<cfset VARIABLES.LoadData() />

		<!--- Return This reference. --->
		<cfreturn THIS />
	</cffunction>


	<cffunction
		name="ClearData"
		access="public"
		returntype="void"
		output="false"
		hint="Clears the stored data.">

		<!--- Clear the instance data. --->
		<cfset StructClear( VARIABLES.Instance.Data ) />

		<!--- Commit the cleared data to file. --->
		<cfset THIS.CommitData() />

		<!--- Return out. --->
		<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.">

		<!--- Define the local scope. --->
		<cfset var LOCAL = StructNew() />

		<!---
			Convert the internal data structure to a WDDX
			xml data structure. Again, not crazy about WDDX,
			but it is easy for our purposes.
		--->
		<cfwddx
			action="CFML2WDDX"
			input="#VARIABLES.Instance.Data#"
			output="LOCAL.FileData"
			/>

		<!---
			Write the data to the data file. We don't care
			if we overwrite an existing data file or just
			create a new one.
		--->
		<cffile
			action="WRITE"
			file="#VARIABLES.Instance.DataFilePath#"
			output="#LOCAL.FileData#"
			/>

		<!--- Return out. --->
		<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.">

		<!--- Define arguments. --->
		<cfargument
			name="Property"
			type="string"
			required="true"
			/>

		<!---
			Check to see if we have a value stored at the
			given property key. If we cannot find the given key,
			then just return the empty string. Think about a
			brain - if you can't remember something, your head
			doesn't explode - ok maybe not, but that's how
			this is going to work.
		--->
		<cfif StructKeyExists(
			VARIABLES.Instance.Data,
			ARGUMENTS.Property
			)>

			<!--- Return the matching property value. --->
			<cfreturn VARIABLES.Instance.Data[ ARGUMENTS.Property ] />

		<cfelse>

			<!---
				No value was found. Return our default
				value which is the empty string.
			--->
			<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.">

		<!--- Define the local scope. --->
		<cfset var LOCAL = StructNew() />

		<!---
			Check to see if our data file eixsts. If it
			does not, then we might have not done anything
			yet (ignore action).
		--->
		<cfif FileExists( VARIABLES.Instance.DataFilePath )>

			<!--- Read in the XML data file. --->
			<cffile
				action="READ"
				file="#VARIABLES.Instance.DataFilePath#"
				variable="LOCAL.FileData"
				/>

			<!---
				Convert the XML data to our internal struct
				data using a WDDX conversion. I am not crazy
				about this WDDX conversion, but it's easy.
			--->
			<cfwddx
				action="WDDX2CFML"
				input="#LOCAL.FileData#"
				output="VARIABLES.Instance.Data"
				/>

		</cfif>

		<!--- Return out. --->
		<cfreturn />
	</cffunction>


	<cffunction
		name="Set"
		access="public"
		returntype="void"
		output="false"
		hint="Sets the given property value at the given key.">

		<!--- Define arguments. --->
		<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."
			/>

		<!--- Store the given value. --->
		<cfset VARIABLES.Instance.Data[ ARGUMENTS.Property ] = ARGUMENTS.Value />


		<!---
			Check to see if we are committing the data. If we
			are not, then skip the commit and leave it up
			to the invoker.
		--->
		<cfif ARGUMENTS.CommitData>

			<!--- Invoke the data commit. --->
			<cfset THIS.CommitData() />

		</cfif>


		<!--- Return out. --->
		<cfreturn />
	</cffunction>

</cfcomponent>

For Cut-and-Paste