Permalink
Browse files

revamp doc factory

  • Loading branch information...
1 parent ecf4451 commit 90f77f9e8e8e3d0e5e87c49ca13392a7dac34857 unknown committed Dec 28, 2009
View
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
- <name>cfmongo</name>
+ <name>cfmongodb</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
+ <nature>org.cfeclipse.cfml.CFENature</nature>
</natures>
</projectDescription>
View
@@ -1,6 +1,7 @@
<cfinterface displayName="IDocument" hint="All MongoDB Documents should implement this interface. Basic ColdFusion structures can also be saved to the datastore. But from an OO design perspective, it's a good practice to implement this interface">
-<cffunction name="setCollection" hint="Sets the collection to which this Document instance belongs. This should be called prior to any other operations. If not, the default Collection, if any, will be the container for this Document."></cffunction>
+<cffunction name="setCollection" hint="Sets the collection to which this Document instance belongs. This should be called prior to any other operations. If not, the default Collection, if any, will be the container for this Document.">
+</cffunction>
<cffunction name="set" hint="Sets a property for the Document" returntype="void">
<cfargument name="property" type="String" />
@@ -15,13 +16,15 @@
<cfargument name="property" hint="Removes the given property if it exists." />
</cffunction>
-<cffunction name="save" hint="Commits this Document instance to the datastore and returns an ID" returntype="String"></cffunction>
+<cffunction name="save" hint="Commits this Document instance to the datastore and returns an ID" returntype="String">
+</cffunction>
<cffunction name="update" hint="Performs in-place updating - Instead of retrieving and updating every item within a document, this method is more efficient, allowing for 'Cherry Picking' of specific data elements to update. ">
<cfargument name="property">
-<cfargument name="value">
+ <cfargument name="value">
</cffunction>
-<cffunction name="validate" hint="Should be called before save()" returntype="void"></cffunction>
+<cffunction name="validate" hint="Should be called before save()" returntype="void">
+</cffunction>
</cfinterface>
View
@@ -64,7 +64,7 @@ function config(){
builder = createObject('component','ExpressionBuilder');
function new_doc(collection_name){
- var document = createObject('component','MongoDocument').init( collection_name, this );
+ var document = createObject('component','MongoDocument').factory_init( collection_name, this );
return document;
}
@@ -1,19 +1,46 @@
-<cfcomponent output="false" implements="IDocument" hint="Default implementation of IDocument and used by the Mongo class to generate a document.">
+<cfcomponent implements="IDocument" output="false" hint="Default implementation of IDocument and used by the Mongo class to generate a document.">
-<cfset this.model = structNew() />
+<cfproperty name="name" default="__DEFAULT_BLANK__" />
+
+<cfset this.model = structNew() >
<cfset variables.mongo = createObject('component', 'MongoDB') />
+<cffunction name="init" hint="Constructor2 - pushes CFPROPERTIES to internal MODEL, which is staged for persitence in datastore.">
+ <cfargument name="collection_name" type="string" required="true" hint="The name of the collection to which this document is bound." />
+ <cfscript>
+ var i = 1;
+ var props = getMetaData().PROPERTIES;
+ var n = '';
+ var prop = '';
+ var val = '';
+ for(i=1; i <= arrayLen(props); i++){
+ prop = props[i];
+ n = prop['NAME'];
+ try{
+ val = prop['DEFAULT'];
+ }
+ catch(Expression e){ //default was not defined
+ val = '';
+ }
+ this.model[n] = val;
+ }
+ variables.mongo.collection(collection_name);
+ </cfscript>
+ <cfreturn this />
+</cffunction>
+
-<cffunction name="init" hint="Constructor. Creates an instance of a MongoDocument.">
- <cfargument name="collection_name" type="string" required="true" hint="The name of the collection to which this document is bound." />
- <cfargument name="_mongo" type="any" required="false" default="#variables.mongo#" hint="The instance of the Mongo wrapper to which the document is bound." />
- <cfset variables.mongo.collection(collection_name) />
- <cfset variables.mongo = _mongo />
- <cfreturn this />
+<cffunction name="factory_init" hint="Constructor. Creates an instance of a MongoDocument using the specified mongo.">
+ <cfargument name="collection_name" type="string" required="true" hint="The name of the collection to which this document is bound." />
+ <cfargument name="_mongo" type="any" required="false" default="#variables.mongo#" hint="The instance of the Mongo wrapper to which the document is bound." />
+ <cfset variables.mongo = _mongo />
+ <cfset variables.mongo.collection(collection_name) />
+ <cfreturn this />
</cffunction>
<cffunction name="setCollection">
+
</cffunction>
@@ -28,14 +55,15 @@
<cfargument name="property" />
<cfset var ret_val = javacast('null', '') />
<cftry>
- <cfset ret_val = this.model[arguments.property] />
- <cfcatch type="Expression">
- <!--- want to return null --->
- </cfcatch>
- </cftry>
+ <cfset ret_val = this.model[arguments.property] />
+ <cfcatch type="Expression">
+ <!--- want to return null --->
+ </cfcatch>
+ </cftry>
<cfreturn ret_val />
</cffunction>
+
<cffunction name="remove" returntype="void" hint="Removes a property form a Document. **NOT IMPLEMENTED**">
<cfargument name="property" hint="Removes the given property if it exists." />
<cfthrow type="NotImplementedException" message="To Do.">
@@ -44,7 +72,8 @@
<cffunction name="save" returntype="String">
- <cfreturn mongo.put(this.model) />
+ <cfset validate() />
+ <cfreturn mongo.put(this.model) />
</cffunction>
@@ -56,12 +85,13 @@
<cffunction name="update" hint="Performs in-place update of the the value for 'property'. NOT IMPLEMENTED">
<cfargument name="property">
<cfargument name="value">
+ <cfset validate() />
<cfthrow type="NotImplementedException" message="To Do.">
</cffunction>
<cffunction name="validate" hint="Should be called before save() to perform any required validation. NOT IMPLEMENTED" returntype="void">
- <cfthrow type="NotImplementedException" message="To Do.">
+
</cffunction>
</cfcomponent>
@@ -1,19 +1,19 @@
<cfcomponent extends="BaseTest">
<cfscript>
-
+
model = createObject('component','fixture.MyMongoModel');
-
-function new_project(){
- project = model.new_project( 'my project', 'owner=123' );
- project.save();
-}
-
function new_person(){
- person = model.new_person( 'bill', '123 main st' );
+ person = model.Person( 'bill', '123 main st' );
person.save();
}
-
+
+function new_project(){
+ var members = [ 1,2,3,4,5,6,7 ];
+ var project = model.Project( 'Cool_101', members );
+ project.save();
+}
+
</cfscript>
</cfcomponent>
@@ -7,8 +7,8 @@
function newDoc(){
doc = createObject('component','fixture.MyMongoDocument').init('my_mongo');
- doc.set('title','my title');
- doc.save();
+ //doc.set('title','my title');
+ //doc.save();
}
View
@@ -0,0 +1,24 @@
+<cfcomponent extends="BaseTest" output="false">
+<cfscript>
+function whatUp(){
+ obj = createObject( 'component','fixture.SomeCFCWithProperties');
+ model = obj['MODEL'];
+ debug( model );
+ assertEquals( model['name'], 'bill' );
+ assertEquals( model['address'], '' );
+ assertEquals( model['city'], '' );
+
+ obj.save();
+
+
+}
+
+function testCreate(){
+ c = createObject( 'component','fixture.SomeCFCWithProperties').init('foo','123 main st','anytown');
+ debug( getMetaData(c) );
+ debug(c);
+
+}
+
+</cfscript>
+</cfcomponent>
@@ -1,4 +1,4 @@
-<cfcomponent persisten="true" extends="cfmongodb.components.MongoDocument" output="false">
+<cfcomponent extends="cfmongodb.components.MongoDocument" output="false">
<cfscript>
/* -------------------------------------------------
** Syntax for defining a persistent model **
@@ -1,26 +1,27 @@
<cfcomponent output="false">
+
<cfscript>
/* -------------------------------------------------
- Example of a condensed model. Instead of separate
+ Example of a "condensed" model. Instead of separate
files for each class, multiple "classes" can be
defined here using the mongo document factory.
----------------------------------------------------*/
-config = {
- server_name = 'localhost',
- server_port = 27017,
- db_name = 'dev',
- collection_name = 'test_coll'
- };
-
-mongo = createObject('component','cfmongodb.components.MongoDb').init(config);
+mongo = createObject('component','cfmongodb.components.MongoDb');
-function new_person(name,addr){
- var person = mongo.new_doc('person',mongo);
- person.set('name',name);
+function Person(name,addr){
+ var person = mongo.new_doc( 'people' );
+ person.set('name', name);
person.set('address',addr);
return person;
}
+
+function Project(name,members){
+ var project = mongo.new_doc( 'projects' );
+ project.set('name', name);
+ project.set('members',members);
+ return project;
+}
</cfscript>
@@ -0,0 +1,17 @@
+<cfcomponent output="true" extends="cfmongodb.components.MongoDocument">
+
+ <cfproperty name="name" default="bill" />
+ <cfproperty name="address" />
+ <cfproperty name="city" />
+ <cfset super.init( collection_name='members' ) />
+
+
+<!--- Representation invariant --->
+ <cffunction name="validate" returntype="void" hint="Called before save(). Should throw exception on failure.">
+ <cfif len( this.model.name) gt 100>
+ <cfthrow type="ModelFuckedUpException" message="hey, punk." />
+ </cfif>
+ </cffunction>
+
+
+</cfcomponent>
View
@@ -0,0 +1,14 @@
+<cfcomponent output="false">
+
+ <cfproperty name="name" type="String" />
+
+ <cffunction name="getName" access="public" output="false" returntype="String">
+ <cfreturn this.name />
+ </cffunction>
+
+ <cffunction name="setName" access="public" output="false" returntype="void">
+ <cfargument name="name" type="String" required="true" />
+ <cfset this.name = arguments.name />
+ <cfreturn />
+ </cffunction>
+</cfcomponent>

0 comments on commit 90f77f9

Please sign in to comment.