Skip to content

Commit

Permalink
Merging in alexanderedge's changes.
Browse files Browse the repository at this point in the history
 * Simplified `lib/spatial.js` (prototypal inheritance FTW)
 * Added a unit test
  • Loading branch information
Dominic Barnes committed Jan 8, 2012
1 parent 7c20c41 commit 597f6c5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 169 deletions.
52 changes: 26 additions & 26 deletions lib/designdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,32 @@ proto.views = function (views) {
return this.body.views;
};

/**
* Set a named view
*
* @param {function} map
* @param {function|string} [reduce]
*
* @return {object} this|view
*/
proto.view = function (name, map, reduce) {
if (map) {
var body = {
map: util.formatFunction(map)
};

if (reduce) {
body.reduce = util.formatFunction(reduce);
}

this.views()[name] = body;

return this;
} else {
return view.create(this, name);
}
};

/**
* Get/set the spatials
*
Expand Down Expand Up @@ -86,32 +112,6 @@ proto.spatials = function (spatials) {
return this.body.spatials;
};

/**
* Set a named view
*
* @param {function} map
* @param {function|string} [reduce]
*
* @return {object} this|view
*/
proto.view = function (name, map, reduce) {
if (map) {
var body = {
map: util.formatFunction(map)
};

if (reduce) {
body.reduce = util.formatFunction(reduce);
}

this.views()[name] = body;

return this;
} else {
return view.create(this, name);
}
};

/**
* Set a named spatial
*
Expand Down
136 changes: 2 additions & 134 deletions lib/spatial.js
Original file line number Diff line number Diff line change
@@ -1,136 +1,4 @@
var client = require("./client"),
doc = require("./document");

// inherit from client
var proto = Object.create(client);

// dependent on url
Object.defineProperty(proto, "name", {
get: function () {
return this._url.path.split("/")[5];
},
set: function (v) {
var path = this._url.path.split("/");
path[5] = v;
this._url.path = this._url.pathname = path.join("/");
}
});

/**
* Perform a generic query against a stored spatial
*
* GET /db/ddoc/_spatial/spatial
*
* @param {object} [query] If only one of the optional params is provided, this is assumed to be the one
* @param {mixed} [data] Array = pass as `keys` in body. Other = pass as complete body
* @param {function} callback
*
* @return {object} this
*/
proto.query = function (query, data, callback) {
if (typeof query === "function") {
callback = query;
data = null;
query = null;
}
if (typeof data === "function") {
callback = data;
data = null;
}

var opts = { query: query || {} };

if (data) {
return this._post(opts, Array.isArray(data) ? { keys: data } : data, callback);
} else {
return this._get(opts, callback);
}
};

/**
* Perform a map query against a stored spatial
*
* GET /db/ddoc/_spatial/spatial
*
* @param {object} [query]
* @param {function} callback
*
* @return {object} this
*/
proto.map = function (query, callback) {
if (typeof query === "function") {
callback = query;
query = {};
}

query.reduce = false;

return this.query(query, callback);
};

/**
* Perform a reduce query against a stored spatial
*
* GET /db/ddoc/_spatial/spatial
*
* @param {object} [query]
* @param {function} callback
*
* @return {object} this
*/
proto.reduce = function (query, callback) {
if (typeof query === "function") {
callback = query;
query = {};
}

query.reduce = true;

return this.query(query, callback);
};

/**
* Execute a list function for the current document
*
* GET /db/_design/design-doc/_list/list-name/doc
*
* @param {string} list The name of the list function in the above design document
* `ddoc/list` ...or... `list` if using same design document as this spatial
* @param {object} [query]
* @param {function} callback
*
* @return {object} this
*/
proto.list = function (list, query, callback) {
if (typeof query === "function") {
callback = query;
query = null;
}

var list_ddoc, spatial, path, url;

if (list.indexOf("/") > -1) {
list = list.split("/");
list_ddoc = list[0];
list = list[1];
spatial = this.ddoc.name + "/" + this.name;
} else {
list_ddoc = this.ddoc.name;
spatial = this.name;
}

path = ["", this.db.name, "_design", list_ddoc, "_list", list, spatial];
url = {
replace: true,
pathname: path.join("/")
};

if (query) {
url.query = query;
}

return this._get(url, callback);
};
var view = require("./view");

/**
* Create a new spatial object
Expand All @@ -141,7 +9,7 @@ proto.list = function (list, query, callback) {
* @return {object} spatial
*/
exports.create = function (ddoc, name) {
var spatial = Object.create(proto);
var spatial = Object.create(view.proto);

spatial.ddoc = ddoc;
spatial.db = ddoc.db;
Expand Down
20 changes: 11 additions & 9 deletions lib/view.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var client = require("./client"),
doc = require("./document");
var client = require("./client");

// inherit from client
var proto = Object.create(client);
Expand Down Expand Up @@ -30,20 +29,20 @@ Object.defineProperty(proto, "name", {
proto.query = function (query, data, callback) {
if (typeof query === "function") {
callback = query;
data = null;
data = null;
query = null;
}
if (typeof data === "function") {
callback = data;
if (typeof data === "function") {
callback = data;
data = null;
}
}

var opts = { query: query || {} };

if (data) {
if (data) {
return this._post(opts, Array.isArray(data) ? { keys: data } : data, callback);
} else {
return this._get(opts, callback);
} else {
return this._get(opts, callback);
}
};

Expand Down Expand Up @@ -132,6 +131,9 @@ proto.list = function (list, query, callback) {
return this._get(url, callback);
};


exports.proto = proto;

/**
* Create a new view object
*
Expand Down
48 changes: 48 additions & 0 deletions test/spatial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var _ = require("underscore"),
test = require("assert"),
config = require("./config"),
couchdb = require("../"),
server = couchdb.srv(config.host, config.port, config.ssl),
db = server.db("test_db_view"),
ddoc = db.ddoc("test_ddoc_1"),
spatial_name = "test_spatial_1";

module.exports = {
before: function (done) {
server.debug = config.debug;
if (!config.party) {
server.setUser(config.user, config.pass);
}
done();
},

after: function (done) {
done();
},

"Spatial": function (done) {
test.ifError(ddoc._body.spatials);

ddoc.spatial(spatial_name, function (doc) {
emit(doc._id);
});

test.ok(ddoc._body.spatials);
test.ok(ddoc._body.spatials[spatial_name]);
test.ok(ddoc._body.spatials[spatial_name].map);
test.ifError(ddoc._body.spatials[spatial_name].reduce);

ddoc.spatial(spatial_name, function (doc) {
emit(doc._id);
}, function (values, key, rereduce) {
return values.length;
});

test.ok(ddoc._body.spatials);
test.ok(ddoc._body.spatials[spatial_name]);
test.ok(ddoc._body.spatials[spatial_name].map);
test.ok(ddoc._body.spatials[spatial_name].reduce);

done();
}
};

0 comments on commit 597f6c5

Please sign in to comment.