Skip to content

Commit

Permalink
Added the functionality to the mongodb model to always return a value…
Browse files Browse the repository at this point in the history
… for attributes even when they don't exist.

This allows for the more fluid documents stored by mongodb.

Closes #2
  • Loading branch information
Zoramite committed Nov 8, 2010
1 parent 37a3e0c commit 9d2a331
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 11 deletions.
62 changes: 60 additions & 2 deletions mongodb/inc/resource/base/model.cfc
@@ -1,4 +1,5 @@
component extends="algid.inc.resource.base.model" {
<cfcomponent extends="algid.inc.resource.base.model" output="false">
<cfscript>
public struct function get__instance() {
var result = super.get__instance();
var key = '';
Expand All @@ -19,4 +20,61 @@ component extends="algid.inc.resource.base.model" {

return result;
}
}
</cfscript>

<cffunction name="onMissingMethod" access="public" returntype="any" output="false">
<cfargument name="missingMethodName" type="string" required="true" />
<cfargument name="missingMethodArguments" type="struct" required="true" />

<cfset var attribute = '' />
<cfset var attributeSet = '' />
<cfset var attributeValue = '' />
<cfset var bundle = '' />
<cfset var childAttribute = '' />
<cfset var format = '' />
<cfset var i = '' />
<cfset var isUnique = '' />
<cfset var j = '' />
<cfset var prefix = '' />
<cfset var result = '' />

<!--- Do a regex on the name --->
<cfset result = reFindNoCase('^(get)(.+)', arguments.missingMethodName, 1, true) />

<!--- If we find don't find anything --->
<cfif not result.pos[1]>
<cfreturn super.onMissingMethod(argumentCollection = arguments) />
</cfif>

<!--- Find the prefix --->
<cfset prefix = mid(arguments.missingMethodName, result.pos[2], result.len[2]) />

<!--- Find the attribute --->
<cfset attribute = mid(arguments.missingMethodName, result.pos[3], result.len[3]) />

<!--- Do the fun stuff --->
<cfswitch expression="#prefix#">
<cfcase value="get">
<cfset result = reFindNoCase('(.+)By(.*)', attribute, 1, true) />

<!--- Check if it is a simple get or a search --->
<cfif not result.pos[1]>
<cfif left(attribute, 2) EQ '__'>
<cfreturn super.onMissingMethod(argumentCollection = arguments) />
</cfif>

<!--- Simple get --->
<cfif structKeyExists(variables.instance, attribute)>
<cfreturn variables.instance[attribute] />
<cfelseif arrayLen(arguments.missingMethodArguments)>
<cfreturn arguments.missingMethodArguments[1] />
</cfif>

<cfreturn '' />
<cfelse>
<cfreturn super.onMissingMethod(argumentCollection = arguments) />
</cfif>
</cfcase>
</cfswitch>
</cffunction>
</cfcomponent>
29 changes: 29 additions & 0 deletions test/inc/resource/base/modelGetShouldTest.cfc
@@ -0,0 +1,29 @@
component extends="mxunit.framework.TestCase" {
public void function setup() {
variables.i18n = createObject('component', 'cf-compendium.inc.resource.i18n.i18n').init(expandPath('/'));
variables.model = createObject('component', 'plugins.mongodb.inc.resource.base.model').init(variables.i18n);
}

/**
* Test that getting a value without setting it returns a blank string.
*/
public void function testReturnBlankWhenNotSet() {
assertEquals('', variables.model.getSomething());
}

/**
* Test that getting a value with a provided default without setting it returns a blank string.
*/
public void function testReturnDefaultWhenNotSet() {
assertEquals('default', variables.model.getSomething('default'));
}

/**
* Test that getting a value with setting it returns the set value.
*/
public void function testReturnValueWhenSet() {
variables.model.setSomething('val');

assertEquals('val', variables.model.getSomething());
}
}
9 changes: 0 additions & 9 deletions test/notATest.cfc

This file was deleted.

0 comments on commit 9d2a331

Please sign in to comment.