Luis,
I implemented the following functions in a requestContextDecorator. I
thought I would share them here. You might not find that these are
appropriate for the core requestContext, but at least you (or anyone
else) will have them if you want them. Implementation notes:
- For dynamic variable setting I used the left side expression in
quotes method recommended here:
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Expressions_18.html
- For dynamic variable getting I had to use Evaluate(). I see you've
used it in other places in ColdBox, but getValue is likely to be
called many times per request.
- For dynamic variable deleting I also used Evaluate and some list
manipulation functions that I'm not sure existed before CF8. I'm not
sure what releases of ColdFusion are supported for ColdBox.
<cffunction name="valueExists" returntype="boolean" access="Public"
output="false"
hint="checks deep in the request collection.">
<cfargument name="name" hint="Name of the variable to find in the
request collection: String" type="any">
<cfargument name="private" type="boolean" required="false"
default="false" hint="Use public or private request collection"/>
<cfscript>
var collection = instance.context;
if( arguments.private ){ collection = instance.privateContext; }
return isDefined('collection.#arguments.name#');
</cfscript>
</cffunction>
<cffunction name="setValue" access="Public" output="false"
returntype="void"
hint="allow setting values deep in the request collection.">
<cfargument name="name" hint="The name of the variable to set.
String" type="any" >
<cfargument name="value" hint="The value of the variable to set"
type="Any" >
<cfargument name="private" type="boolean" required="false"
default="false" hint="Use public or private request collection"/>
<cfscript>
var collection = instance.context;
if( arguments.private ) { collection = instance.privateContext; }
'collection.#arguments.name#' = arguments.value;
</cfscript>
</cffunction>
<cffunction name="getValue" returntype="Any" access="Public"
output="false"
hint="returns a value from deep in the request collection.">
<cfargument name="name" type="any" required="true"
hint="Name of the variable to get from the request collection">
<cfargument name="defaultValue" type="any" required="false"
default="NONE" hint="Default value to return if not found.">
<cfargument name="private" type="boolean" required="false"
default="false" hint="Use public or private request collection"/>
<cfscript>
var collection = instance.context;
var returnVal = '';
if( arguments.private ){ collection = instance.privateContext; }
if( valueExists(name=arguments.name, private=arguments.private) ){
return evaluate('collection.#arguments.name#');
}
else if ( isSimpleValue(arguments.defaultValue) and
arguments.defaultValue eq "NONE" ){
$throw("The variable: #arguments.name# is undefined in the request
collection.",
"Default: #arguments.defaultValue#,
Private:#arguments.private#, Keys
#structKeyList(collection)#","RequestContext.ValueNotFound");
}
else{
return arguments.defaultValue;
}
</cfscript>
</cffunction>
<cffunction name="removeValue" access="Public" hint="I remove a value
in the request collection" output="false" returntype="any">
<cfargument name="name" hint="The name of the variable to remove."
type="string" >
<cfargument name="private" type="boolean" required="false"
default="false" hint="Use public or private request collection"/>
<cfscript>
var collection = instance.context;
var location = listDeleteAt(name, listLen(name, "."), ".");
if( arguments.private ){ collection = instance.privateContext; }
if( valueExists(arguments.name, arguments.private) ){
return structDelete(evaluate('collection.#location#'),
listLast(arguments.name, "."), true);
}
</cfscript>
</cffunction>