Skip to content

Instantly share code, notes, and snippets.

@adamcameron
adamcameron / ClassViewer.cfc
Created August 7, 2012 18:28
An almost pure CFML implementation of ClassViewer.class (see http://adamcameroncoldfusion.blogspot.co.uk/2012/08/classviewerjava.html).
/**
ClassViewer.cfc
@hint An almost pure CFML implementation of ClassViewer.class (see http://adamcameroncoldfusion.blogspot.co.uk/2012/08/classviewerjava.html).
@description
*/
component {
// these are just for formatting the output
variables._NL = createObject("java", "java.lang.System").getProperty("line.separator");
variables._INDENT = " ";
@adamcameron
adamcameron / medalTable.cfm
Created August 13, 2012 07:44
Code for generating my medal tables (see: http://bit.ly/RG6FLD)
<cfset CRLF = chr(13) & chr(10)>
<cfsavecontent variable="rendered">
<cfsetting enablecfoutputonly="true">
<cfoutput>
<style>
<!--- these are inline because I actually need the styles, not a CSS link due to a vagary of the blog --->
<cfinclude template="./styles.css">
</style>
</cfoutput>
<cfoutput><table border="0" cellpadding="0" cellspacing="0" valign="top"></cfoutput>
@adamcameron
adamcameron / Application.cfc
Created August 15, 2012 22:04
An Application.cfc which logs when various variables become available
component {
param name="server.hitId" default=0;
server.hitId++;
testScopes("in pseudoconstructor before application name is set", "pseudoconstructor");
this.name = "scopeAvailability";
this.sessionManagement = true;
testScopes("in pseudoconstructor after application name is set", "pseudoconstructor");
function onApplicationStart(){
@adamcameron
adamcameron / structToKeys.cfm
Created September 18, 2012 15:57
Function to recursively get all structkeys and convert to dot notation
<cfscript>
// data to test with
st = {
top1 = "top1 value",
top2 = {
middle = {
bottom1 = [1,2,3],
bottom2 = {},
bottom3 = "bottom3 value"
}
@adamcameron
adamcameron / Application.cfc
Created September 19, 2012 17:56
An Application.cfc to test what happens when this.datasource is set by a conditional using a CGI variable
component {
this.name = "testPseudoConstructor";
this.applicationTimeout = createTimeSpan(0,0,1,0);
this.sessionManagement = true;
this.sessionTimeout = createTimeSpan(0,0,0,30);
if (CGI.server_name == "localhost"){
this.datasource = "dsn1";
}else if (CGI.server_name == "testing.local"){
@adamcameron
adamcameron / paths.cfm
Created September 21, 2012 07:50
Demonstrating some (possible) weirdness with expandPath()
<cfoutput>
#expandPath(".")#<br /> <!--- for me outputs D:\websites\www.scribble.local\junk --->
#expandPath("/")#<br /> <!--- for me outputs d:\websites\www.scribble.local\. Note the difference in capitalisation of the drive letter --->
</cfoutput>
@adamcameron
adamcameron / baseMToBaseN.cfm
Created September 24, 2012 14:10
Converts a number from one arbitrary base to another arbitrary base. Is not restricted to bases that Java and CF natively support in their equivalent built-in functions. The internal maths are restricted to the bounds of java.math.BigInteger though.
/**
@hint Converts a number from one arbitrary base to another arbitrary base. Is not restricted to bases that Java and CF natively support in their equivalent built-in functions. The internal maths are restricted to the bounds of java.math.BigInteger though.
@number The number to convert.
@fromBase The base to convert from. Can either be one of BIN, DEC, HEX, BASE36, BASE62 or an 'alphabet' or characters that represent the digits. EG: OCTAL would be 01234567.
@toBase The base to convert to. Has same value rules as fromBase.
*/
string function baseMToBaseN(required string number, required string fromBase, required string toBase){
if (fromBase == toBase){ // ie: there's nothing to do
return number;
}
@adamcameron
adamcameron / testBaseMToBaseN.cfm
Created September 24, 2012 14:13
This is to be used in conjunction with https://gist.github.com/3776135
// testing
test.description = "A fairly easy to visually-test example";
test.number = 255;
test._baseFrom = "dec";
test._baseTo = "bin";
test._result = baseMToBaseN(test.number, test._baseFrom, test._baseTo);
writeDump(test);
test.description = "Another one";
@adamcameron
adamcameron / dateNow.cfm
Created September 24, 2012 19:05
How to create a java.sql.Date in CF
dateNow = createObject("java", "java.sql.Date").init(dateDiff("s", createDate(1970,1,1), now())*1000);
@adamcameron
adamcameron / renderWith.cfm
Last active October 11, 2015 02:37
This is a fairly contrived example of closure in CFML
<cfscript>
/**
@hint creates a function which will wrap passed-in text with the specified tag. Any addition arguments passed to this function will be treated as attribute/value pairs of the tag
@tag The tag to wrap the function's text in
*/
string function renderWith(required string tag){
var theTag = "";
var attributes = "";
var attribute = "";