Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created June 4, 2021 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/599ef4efa6e42c363747538aa66f8a60 to your computer and use it in GitHub Desktop.
Save bennadel/599ef4efa6e42c363747538aa66f8a60 to your computer and use it in GitHub Desktop.
Changing Function Argument Defaults At Runtime In Lucee CFML 5.3.7.47
component
output = false
hint = "I provide random integers between a given set of values."
{
/**
* I initialize the randomizer with the given min/max range.
*/
public void function init(
numeric minValue = 1,
numeric maxValue = 100
) {
variables.defaults = {
minValue: minValue,
maxValue: maxValue
};
}
// ---
// PUBLIC MEHTODS.
// ---
/**
* I get the next random value within the given min/max range. If no range is
* provided, uses default range.
*/
public numeric function next(
numeric minValue = defaults.minValue,
numeric maxValue = defaults.maxValue
) {
return( randRange( minValue, maxValue ) );
}
/**
* I update the default range with the given min/max values.
*/
public any function withDefaults(
required numeric minValue,
required numeric maxValue
) {
defaults.minValue = minValue;
defaults.maxValue = maxValue;
return( this );
}
}
<cfscript>
rando = new RandomValue()
.withDefaults( 1, 10 )
;
</cfscript>
<cfscript>
password = new Password()
.withArgon2Defaults(
iterations = 10,
outputLength = 32
)
.withBCryptDefaults(
costFactor = 11
)
.withSCryptDefaults(
resources = 3,
parallelisation = 2,
outputLength = 32
)
;
</cfscript>
<cfscript>
rando = new RandomValue( 1, 10 );
echoLine( "With range, 1 - 10" );
echoLine( rando.next() );
echoLine( rando.next() );
echoLine( rando.next() );
echoLine( rando.next() );
echoLine();
rando.withDefaults( 50, 100 );
echoLine( "With range, 50 - 100" );
echoLine( rando.next() );
echoLine( rando.next() );
echoLine( rando.next() );
echoLine( rando.next() );
echoLine();
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
/**
* I write the given arguments to a single line, followed by a break-tag.
*/
public void function echoLine() {
echo( arrayToList( arguments, " " ) & "<br />" );
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment