Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
romanmt committed Jun 27, 2012
0 parents commit 42a4434
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
copyright (c) 2012 Jamplify

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Empty file added README.md
Empty file.
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = exports = function superGoosePlugin(schema) {
schema.statics.findOrCreate = function findOrCreate(object, callback) {
var self = this;
this.findOne(object, function(err, result) {
if(err || result)
callback(err, result)
else
self.create(object, callback)
})
}
}

21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "supergoose"
, "description": "Mongoose plugin for simple add ons like find or create"
, "version": "1.0.0"
, "author": "Matt Roman"
, "keywords": ["supergoose", "mongodb", "mongoose", "plugin", "findOrCreate"]
, "repository": {
"type": "git"
, "url": "git://github.com/jamplify/supergoose"
}
, "devDependencies": {
"should": ">= 0.2.1"
, "mocha": ">= 0.13.0"
, "sinon": ">= 1.3.1"
, "mongoose": ">= 2.6.5"
}
, "license": "MIT"
, "engine": {
"node": ">= 0.6"
}
}
33 changes: 33 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var mocha = require('mocha')
, should = require('should')
, sinon = require('sinon')
, mongoose = require('mongoose')
, Schema = mongoose.Schema
, supergoose = require('../index.js')

var ClickSchema = new Schema({
ip : String
})

ClickSchema.plugin(supergoose);
var Click = mongoose.model('Click', ClickSchema);

describe('findOrCreate', function() {
it("should return the object if it exists", sinon.test(function(done) {
this.stub(Click, 'findOne').yields(null, {ip: '127.0.0.1'})
Click.findOrCreate({ip: '127.0.0.1'}, function(err, click) {
click.should.eql({ip: '127.0.0.1'})
})
}))

it("check to see if the object exists", sinon.test(function() {
this.mock(Click).expects('findOne')
Click.findOrCreate({ip: '127.0.0.1'}, function(err, click) {})
}))

it("should call create if it doesn't exist", sinon.test(function() {
this.stub(Click, 'findOne').yields(null, null);
this.stub(Click, 'create').withArgs({ip: '127.0.0.1'})
Click.findOrCreate({ip: '127.0.0.1'}, function(err, click) {})
}))
})

0 comments on commit 42a4434

Please sign in to comment.