Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove works from hasMany Added documentation
- Loading branch information
Scott Burch
committed
Jan 30, 2012
1 parent
f4cb26b
commit 16ac299
Showing
10 changed files
with
457 additions
and
251 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* The base constructor that is extended by each domain constructor | ||
* Contains the basic methods save/remove... | ||
*/ | ||
"use strict"; | ||
module.exports = function(db, name, config) { | ||
var domain = require('couch-ar'); | ||
var helpers = require('./helpers'); | ||
var that = {}; | ||
|
||
configureHasMany(); | ||
|
||
|
||
that.serialize = function() { | ||
var obj = Object.getOwnPropertyNames(config.properties).reduce(function(obj, prop) { | ||
obj[prop] = that[prop]; | ||
return obj; | ||
}, {}); | ||
obj.type = name; | ||
obj._id = obj.id; | ||
obj._rev = obj.rev; | ||
return obj; | ||
} | ||
|
||
that.save = function(callback) { | ||
callback = callback || function() { | ||
} | ||
that.beforeSave && that.beforeSave(); | ||
var out = that.serialize(); | ||
that.dateCreated = that.dateCreated || new Date(); | ||
that.lastUpdated = new Date(); | ||
db.save(that.id, that.serialize(), function(err, res) { | ||
if (res.ok) { | ||
that.id = res.id; | ||
that.rev = res.rev | ||
} | ||
callback(err, res); | ||
}); | ||
} | ||
|
||
that.remove = function(callback) { | ||
if (that.id) { | ||
db.remove(that.id, that.rev, function(err, res) { | ||
that.id = err ? that.id : undefined; | ||
callback(err, res); | ||
}); | ||
} else { | ||
callback(); | ||
} | ||
} | ||
return that; | ||
|
||
function configureHasMany() { | ||
|
||
Object.keys(config.hasMany || {}).forEach(function(propName){ | ||
var singularPropName = propName.replace(/(.*)s$/,'$1'); | ||
var upperPropName = helpers.toUpper(propName); | ||
var singularUpperPropName = helpers.toUpper(singularPropName); | ||
var idsArray = that[singularPropName + 'Ids'] = []; | ||
var model = domain[config.hasMany[propName]]; | ||
addGetter(); | ||
addAdder(); | ||
addRemover(); | ||
|
||
function addGetter() { | ||
that['get' + upperPropName] = function(cb) { | ||
var count = 0; | ||
var things = []; | ||
var ids = idsArray.slice(0); | ||
ids.length === 0 && cb([]); | ||
ids.forEach(function(id) { | ||
model.findById(id, function(thing) { | ||
things.push(thing); | ||
count++; | ||
count === ids.length && cb(things); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
function addAdder() { | ||
that['add' + singularUpperPropName] = function(it) { | ||
if(it.id === undefined) { | ||
throw 'Can not add non-persisted entity to hasMany'; | ||
} | ||
idsArray.indexOf(it.id) === -1 && idsArray.push(it.id); | ||
} | ||
} | ||
|
||
function addRemover() { | ||
that['remove' + singularUpperPropName] = function(it) { | ||
var idx = idsArray.indexOf(it.id); | ||
if(idx !== -1) { | ||
idsArray.splice(idx,1); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
toUpper: function(s) { | ||
return s[0].toUpperCase() + s.slice(1); | ||
} | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/sh | ||
export NODE_PATH=`pwd`/../node_modules:`pwd`/../ | ||
export NODE_PATH=`pwd`/../node_modules/jasmine-node/lib:`pwd`/../ | ||
../node_modules/jasmine-node/bin/jasmine-node spec | ||
|
Oops, something went wrong.