Created
April 6, 2011 20:50
-
-
Save seancoyne/906496 to your computer and use it in GitHub Desktop.
A simple ColdFusion scope facade
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- | |
| 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