Permalink
Browse files

[wip] WIP

  • Loading branch information...
1 parent 03b5f23 commit f3d86d06c150d778098fc6fe343481f86bcc3af9 @mmalecki mmalecki committed Jun 17, 2012
Showing with 39 additions and 16 deletions.
  1. +39 −16 lib/resourceful/engines/memory.js
@@ -106,26 +106,49 @@ Memory.prototype.destroy = function (key, callback) {
};
Memory.prototype.find = function (conditions, callback) {
- this.filter(function (obj) {
- return Object.keys(conditions).every(function (k) {
- return conditions[k] === obj[k];
- });
- }, callback);
-};
+ var self = this;
-Memory.prototype.filter = function (filter, callback) {
this.request(function () {
- var result = [],
- store = this.store;
-
- Object.keys(this.store).forEach(function (k) {
- if (filter(store[k])) {
- result.push(store[k]);
- }
+ var results = [];
+ Object.keys(self.store).forEach(function (key) {
+ var obj = self.store[key];
+ Object.keys(conditions).forEach(function (k) {
+ if (conditions[k] === obj[k]) {
+ results.push(obj);
+ }
+ });
});
-
- callback(null, result);
+ callback(null, results);
});
+}
+
+Memory.prototype.filter = function (name, data) {
+ var self = this,
+ map = typeof data.filter === 'function' ? data.filter : data.filter.map;
+
+ map = new Function([
+ 'return (function (_doc, emit) {',
+ ' (' + map.toString() + ')(_doc)',
+ '})'
+ ].join('\n'));
+
+ data.resource[name] = function (callback) {
+ var results = [];
+
+ //
+ // Mimic CouchDB's view API.
+ //
+ function emit(id, doc) {
+ results.push(self.store[doc._id]);
+ }
+
+ self.request(function () {
+ Object.keys(self.store).forEach(function (key) {
+ map()(self.store[key], emit);
+ });
+ callback(null, results);
+ });
+ };
};
Memory.prototype.sync = function (factory, callback) {

0 comments on commit f3d86d0

Please sign in to comment.