ColdFusion UDF Library - reScope

December 16th, 2008
«« Back to ColdFusion Library

Category:
   SYSTEM

Description:
   Copies all variables in one scope to another

Parameters:
  1. newScope (String, required)
           The scope to place all variables of the specified scopes into.

  2. scope (String, required)
           The scope to copy variables from. This can be specified one or more times.

  3. overwrite (Boolean, Default: False)
           Overwrite the the variable in the new scope, if it is already defined in the new scope. Default false.


Returns:
       

Requires:
       

Sample Use:
       

Copy to Clipboard
function reScope(newScope){
	var keys = arrayNew(1);
	var loop = 0;
	var loop2 = 0;
	var valid = "^[a-z_]\w*$";
	var invalid = "\W*";
	var variable = "";
	var overwrite = false;
	var end = arrayLen(arguments);
	if (isSimpleValue(newScope)){
		if (isBoolean(arguments[arrayLen(arguments)])){
			overwrite = arguments[arrayLen(arguments)];
			end = arrayLen(arguments)-1;
		};
		for (loop = 2; loop lte end; loop = incrementValue(loop)){
			keys = structKeyArray(arguments[loop]);
			for (loop2 = 1; loop2 lte arrayLen(keys); loop2 = incrementValue(loop2)){
				variable = keys[loop2];
				if (not reFindNoCase(valid,variable))variable = trim(reReplaceNoCase(variable,invalid,"","all"));
				if (reFindNoCase(valid,variable) and (not isDefined("#newScope#.#variable#") or overwrite))setVariable("#newScope#.#variable#",arguments[loop][keys[loop2]]);
			};
		};
	}
	else writeOutput("New scope passed to reScope must be a string.");
};
// http://www.programmers.org/index.php/coldfusion-udf-library/?ckey=91&function=reScope
  1. No comments yet.
  1. No trackbacks yet.