Anybody think of a reason why this shouldn't work,...
# cfml-general
m
Anybody think of a reason why this shouldn't work, to be able to pass an object a Struct and have it bulk-update the fields of the object?
Copy code
public Boolean function setMemento( required Struct updates )
	{
		var setter = "";
		var value  = "";

		for(var key in updates)
		{
			setter = "set#Key#";
			value = updates[key];
			invoke( this, setter, value );
		}

		return true;
	}
It's not throwing any errors, but it's also not updating the values.
all values are simple (no objects, arrays, structs, etc.). and this does work:
Copy code
public Boolean function setMemento( required Struct updates )
	{
		var key    = "";
		var setter = "";
		var value  = "";

		for( key in updates)
		{
			setter = "set#key#";
			value  = updates[key];
			Evaluate("this.#setter#(value)");
		}

		return true;
	}
a
What if you go:
Copy code
setter = this["set#key#"];
			value  = updates[key];
			setter(value);
?
I'd expect the
invoke
version to run or error. Not do f***-all. But I also don't find it surprising.
m
it's adobe, so yeah. haven't tried the other. it's just for a very specific test suite, so not critical as long as i have it working, we just weirded out that invoke isn't doing anything.
i do like the elegance of your solution, may use that.
a
Hang-on, don't get ahead of myself. I didn't test that. It was just to factor
invoke
and
evaluate
out of the equation
m
yes, and it does work, just tested it.
Copy code
public Boolean function setMemento( required Struct updates )
	{
		for( var key in updates)
		{
			var setter = this["set#key#"];
			var value  = updates[key];
			setter(value);
		}

		return true;
	}
m
Copy code
if ( structKeyExists( variables, "get#useDataModel#" ) ) {
	dat = variables[ "get#useDataModel#" ]( argumentCollection=modelargs );				
}
// i do something pretty similar, likely would easily adapt, like Adam, I didn't want evaluate or invoke