Skip to content

Instantly share code, notes, and snippets.

@seancoyne
Created April 6, 2011 20:50
Show Gist options
  • Select an option

  • Save seancoyne/906496 to your computer and use it in GitHub Desktop.

Select an option

Save seancoyne/906496 to your computer and use it in GitHub Desktop.
A simple ColdFusion scope facade
<!---
Scope Facade
Author: Sean Coyne (http://n42designs.com)
Copyright 2011 and Beyond. All Rights Reserved.
--->
<cfcomponent output="false">
<cfset variables.instance = {} />
<cfset variables.instance.scopename = '' />
<cfset variables.instance.scope = '' />
<cffunction name="init" returntype="scopeFacade" output="false" access="public">
<cfargument name="scope" required="true" type="variablename" />
<cfset variables.instance.scopename = arguments.scope />
<cfreturn this />
</cffunction>
<cffunction name="getscope" returntype="struct" output="false" access="private">
<cfreturn structGet(variables.instance.scopename) />
</cffunction>
<cffunction name="get" returntype="any" output="false" access="public">
<cfargument name="key" required="true" type="string" />
<cfargument name="default" required="false" type="string" />
<cfset var scope = getscope() />
<cfif structKeyExists(arguments,"default") and not structKeyExists(scope,arguments.key)>
<cfreturn arguments.default />
</cfif>
<cfreturn scope[arguments.key] />
</cffunction>
<cffunction name="set" returntype="void" output="false" access="public">
<cfargument name="key" required="true" type="string" />
<cfargument name="value" required="true" type="any" />
<cfset var scope = getscope() />
<cfset scope[arguments.key] = arguments.value />
</cffunction>
<cffunction name="delete" returntype="void" output="false" access="public">
<cfargument name="key" required="true" type="string" />
<cfset var scope = getscope() />
<cfif exists(arguments.key)>
<cfset structDelete(scope,arguments.key) />
</cfif>
</cffunction>
<cffunction name="exists" returntype="boolean" output="false" access="public">
<cfargument name="key" required="true" type="string" />
<cfreturn structKeyExists(getScope(),arguments.key) />
</cffunction>
</cfcomponent>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment