Skip to content

Instantly share code, notes, and snippets.

TryCF Gist
<cfscript>
// Set up the query object to represent our rates.
myQuery = queryNew(
"code, rate",
"integer, integer",
[
{ "code" : 1 , "rate" : 10 } ,
{ "code" : 2 , "rate" : 15 } ,
{ "code" : 3 , "rate" : 20 } ,
{ "code" : 4 , "rate" : 25 } ,
{ "code" : 5 , "rate" : 30 }
]
);
//writeDump(myQuery) ;
function returnNewRates( required Numeric origCode, required Numeric toCode ) {
local.ratesStruct = { "origRate":-1, "toRate":-1 } ;
// This will be our query. If we _have_ to use an existing query, pass it in and duplicate() it. (Pass by Reference!)
local.qry = duplicate( myQuery ) ;
/////////////
// Closure to filter the query. This should be done in SQL.
local.filteredQuery = qry
.filter( function (obj) {
return ( obj.code == origCode || obj.code == toCode ) ;
} ) ;
// Now assign new rates. NOTE: The query shouldn't return more than 2 rows. We can validate if needed.
for ( var r IN filteredQuery ) {
if( r.code == arguments.origCode ) { ratesStruct.origRate = r.rate ; }
if( r.code == arguments.toCode ) { ratesStruct.toRate = r.rate ; }
}
return ratesStruct ;
}
//// TESTS ////
writedump( returnNewRates(1, 2) ) ; // Get 2 valid values.
writedump( returnNewRates(2, 9) ) ; // Get 1 invalid value.
writedump( returnNewRates(3, 3) ) ; // Get same value for both.
writedump( returnNewRates(1, 2) ) ; // We didn't change the base query?
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment