<cfcomponent
output="false"
hint="Provides generic getter and setter methods based on intance properties and spoof methods.">
<cfset VARIABLES.PropertyList = {
Get = "",
Set = ""
} />
<cffunction
name="Init"
access="public"
returntype="any"
output="false"
hint="Returns an intialized component.">
<cfset var LOCAL = {} />
<cfset LOCAL.Methods = GetMetaData( THIS ).Functions />
<cfloop
index="LOCAL.Method"
array="#LOCAL.Methods#">
<cfif (
StructKeyExists( LOCAL.Method, "kinky:type" ) AND
(LOCAL.Method[ "kinky:type" ] EQ "spoof")
)>
<cfset LOCAL.PropertyMethod = Left(
LOCAL.Method.Name,
3
) />
<cfset LOCAL.PropertyName = Right(
LOCAL.Method.Name,
(Len( LOCAL.Method.Name ) - 3)
) />
<cfif StructKeyExists( VARIABLES.Instance, LOCAL.PropertyName )>
<cfset VARIABLES.PropertyList[ LOCAL.PropertyMethod ] = ListAppend(
VARIABLES.PropertyList[ LOCAL.PropertyMethod ],
LOCAL.PropertyName
) />
<cfset StructDelete( THIS, LOCAL.Method.Name ) />
</cfif>
</cfif>
</cfloop>
<cfreturn THIS />
</cffunction>
<cffunction
name="Get"
access="public"
returntype="any"
output="false"
hint="Generic getter for valid instance properties.">
<cfargument
name="Property"
type="string"
required="true"
hint="The property name being retreived."
/>
<cfif ListFindNoCase(
VARIABLES.PropertyList.Get,
ARGUMENTS.Property
)>
<cfreturn VARIABLES.Instance[ ARGUMENTS.Property ] />
<cfelse>
<cfthrow
type="Instance.InvalidProperty"
message="Invalid property"
detail="The property you requested, #UCase( ARGUMENTS.Property )#, is not a valid property for the generic getter."
/>
</cfif>
</cffunction>
<cffunction
name="Set"
access="public"
returntype="any"
output="false"
hint="Generic setter for valid instance properties.">
<cfargument
name="Property"
type="string"
required="true"
hint="The property name being set."
/>
<cfargument
name="Value"
type="any"
required="true"
hint="The property value being set."
/>
<cfif ListFindNoCase(
VARIABLES.PropertyList.Set,
ARGUMENTS.Property
)>
<cfset VARIABLES.Instance[ ARGUMENTS.Property ] = ARGUMENTS.Value />
<cfreturn THIS />
<cfelse>
<cfthrow
type="Instance.InvalidProperty"
message="Invalid property"
detail="The property you set, #UCase( ARGUMENTS.Property )#, is not a valid property for the generic setter."
/>
</cfif>
</cffunction>
<cffunction
name="OnMissingMethod"
access="public"
returntype="any"
output="false"
hint="Used to proxy the generic getter and setter methods.">
<cfargument
name="MissingMethodName"
type="string"
required="true"
hint="The name of the requested method."
/>
<cfargument
name="MissingMethodArguments"
type="struct"
required="true"
hint="The struct of arguments."
/>
<cfset var LOCAL = {} />
<cfset LOCAL.PropertyMethod = Left(
ARGUMENTS.MissingMethodName,
3
) />
<cfset LOCAL.PropertyName = Right(
ARGUMENTS.MissingMethodName,
(Len( ARGUMENTS.MissingMethodName ) - 3)
) />
<cfif (LOCAL.PropertyMethod EQ "Get")>
<cfreturn THIS.Get( LOCAL.PropertyName ) />
<cfelseif (LOCAL.PropertyMethod EQ "Set")>
<cfreturn THIS.Set(
LOCAL.PropertyName,
ARGUMENTS.MissingMethodArguments[ 1 ]
) />
<cfelse>
<cfthrow
type="Component.MissingMethod"
message="Method does not exist"
detail="The method you are attempting to access, #UCase( ARGUMENTS.MissingMethodName )#, is not a valid method of this component."
/>
</cfif>
</cffunction>
</cfcomponent>