From ff36dbaeed0c6531e7bdf1e4ec512c494d1bd896 Mon Sep 17 00:00:00 2001 From: Pavan Kumar Sunkara Date: Sun, 15 Apr 2012 01:23:50 +0530 Subject: [PATCH] [minor] Fix whitespace --- lib/resourceful/cache.js | 4 ++-- lib/resourceful/definers.js | 4 ++-- lib/resourceful/engines/couchdb/index.js | 26 ++++++++++++------------ lib/resourceful/engines/memory.js | 2 +- lib/resourceful/init.js | 2 +- lib/resourceful/resource.js | 20 +++++++++--------- package.json | 2 +- 7 files changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/resourceful/cache.js b/lib/resourceful/cache.js index 426b887..df210dd 100644 --- a/lib/resourceful/cache.js +++ b/lib/resourceful/cache.js @@ -27,7 +27,7 @@ exports.Cache.prototype.get = function (id) { return id.map(function (k) { return that.store[k.toString()]; }); - } + } else { return this.store[id.toString()]; } @@ -54,7 +54,7 @@ exports.Cache.prototype.clear = function (id) { if (id) { this.size --; delete(this.store[id]); - } + } else { this.size = 0; this.store = {}; diff --git a/lib/resourceful/definers.js b/lib/resourceful/definers.js index 60ce162..f657018 100644 --- a/lib/resourceful/definers.js +++ b/lib/resourceful/definers.js @@ -163,9 +163,9 @@ module.exports = { }), prefix: function (prefix) { var matcher = new RegExp('^' + prefix + '(.*)'); - + return this.sanitize(function (val) { - return !matcher.test(val) + return !matcher.test(val) ? prefix + val : val; }); diff --git a/lib/resourceful/engines/couchdb/index.js b/lib/resourceful/engines/couchdb/index.js index 726bbe9..dc4dc95 100644 --- a/lib/resourceful/engines/couchdb/index.js +++ b/lib/resourceful/engines/couchdb/index.js @@ -28,7 +28,7 @@ var Couchdb = exports.Couchdb = function Couchdb(config) { secure: config.secure, auth: config && config.auth || null }).database(config.database || resourceful.env); - + this.cache = new resourceful.Cache(); }; @@ -53,8 +53,8 @@ Couchdb.prototype.get = function (id, callback) { e.status = e.headers.status; return callback(e); } - - return Array.isArray(id) + + return Array.isArray(id) ? callback(null, res.rows.map(function (r) { return r.doc; })) : callback(null, res); }); @@ -63,7 +63,7 @@ Couchdb.prototype.get = function (id, callback) { Couchdb.prototype.put = function (id, doc, callback) { return this.request('put', id, doc, function (e, res) { if (e) return callback(e); - + res.status = 201; callback(null, resourceful.mixin({}, doc, res)); }); @@ -82,12 +82,12 @@ Couchdb.prototype.save = function (id, doc, callback) { var args = Array.prototype.slice.call(arguments, 0), callback = args.pop(), doc = args.pop(); - + // if there's an ID left in args after popping off the callback and // the doc, then we need to PUT, otherwise create a new record thru POST if (args.length) { return this.put.apply(this, arguments); - } + } // checks for presence of _id in doc, just in case the caller forgot // to add an id as first argument @@ -98,7 +98,7 @@ Couchdb.prototype.save = function (id, doc, callback) { } }; Couchdb.prototype.update = function (id, doc, callback) { - return this.cache.has(id) + return this.cache.has(id) ? this.put(id, resourceful.mixin({}, this.cache.get(id).toJSON(), doc), callback) : this.request('merge', id, doc, callback); }; @@ -116,17 +116,17 @@ Couchdb.prototype.destroy = function () { if (rev) { return this.request.apply(this, ['remove', id, rev, callback]); - } + } if (this.cache.has(id)) { args.splice(1, -1, this.cache.get(id)._rev); return this.request.apply(this, ['remove'].concat(args)); - } - + } + this.head(id, function (e, headers, res) { if (res === 404 || !headers['etag']) { e = e || { reason: 'not_found' }; } - + if (headers.etag) { args.splice(1, -1, headers.etag.slice(1, -1)); return that.request.apply(that, ['remove'].concat(args)); @@ -176,14 +176,14 @@ Couchdb.prototype.sync = function (factory, callback) { if (!e && headers.etag) { factory._design._rev = headers.etag.slice(1, -1); } - + that.connection.put(id, factory._design, function (e, res) { if (e) { if (e.reason === 'no_db_file') { that.connection.create(function () { that.sync(factory, callback); }); - } + } else { /* TODO: Catch errors here. Needs a rewrite, because of the race */ diff --git a/lib/resourceful/engines/memory.js b/lib/resourceful/engines/memory.js index 8181007..bdcd8f5 100644 --- a/lib/resourceful/engines/memory.js +++ b/lib/resourceful/engines/memory.js @@ -9,7 +9,7 @@ var Memory = exports.Memory = function (options) { var counter = 0; options = options || {}; this.uri = options.uri; - + this.increment = function () { return ++counter; } diff --git a/lib/resourceful/init.js b/lib/resourceful/init.js index e14f97f..8c3e686 100644 --- a/lib/resourceful/init.js +++ b/lib/resourceful/init.js @@ -3,7 +3,7 @@ module.exports = function init(obj, property, schema) { if (obj[property]) { return obj[property]; } - + return !!init.type[schema.type] ? init.type[schema.type](schema) : null; diff --git a/lib/resourceful/resource.js b/lib/resourceful/resource.js index bbdf3df..2089358 100644 --- a/lib/resourceful/resource.js +++ b/lib/resourceful/resource.js @@ -8,10 +8,10 @@ var util = require('util'), // var Resource = exports.Resource = function () { Object.defineProperty(this, 'isNewRecord', { - value: true, + value: true, writable: true }); - + Object.defineProperty(this, 'schema', { value: this.constructor.schema, enumerable: false, @@ -175,7 +175,7 @@ Resource.get = function (id, callback) { id = this.schema.properties._id.sanitize(id); } - return id + return id ? this._request('get', id, callback) : callback && callback(new Error('key is undefined')); }; @@ -245,8 +245,8 @@ Resource.destroy = function (id, callback) { if (this.schema.properties._id && this.schema.properties._id.sanitize) { id = this.schema.properties._id.sanitize(id); } - - return id + + return id ? this._request('destroy', id, callback) : callback && callback(new Error('key is undefined')); }; @@ -255,12 +255,12 @@ Resource.update = function (id, obj, callback) { if (this.schema.properties._id && this.schema.properties._id.sanitize) { id = this.schema.properties._id.sanitize(id); } - + if (this._timestamps) { obj.mtime = Date.now(); } - - return id + + return id ? this._request('update', id, obj, callback) : callback && callback(new Error('key is undefined')); }; @@ -610,7 +610,7 @@ Resource.prototype.update = function (obj, callback) { Resource.prototype.saveAttachment = function (attachment, callback) { return this.constructor.saveAttachment({ - id: this._id, + id: this._id, rev: this._rev }, attachment, callback); }; @@ -659,7 +659,7 @@ Resource.prototype.__defineGetter__('key', function () { }); Resource.prototype.__defineGetter__('id', function () { - return this.constructor.key === '_id' + return this.constructor.key === '_id' ? this._id : undefined; }); diff --git a/package.json b/package.json index 7c3f8b7..6ea66f3 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ }, "dependencies": { "cradle": "0.6.x", - "revalidator": "0.1.x" + "revalidator": "0.1.x" }, "devDependencies": { "vows": "~0.6.0"