Skip to content

Commit 7d34c7d

Browse files
committed
Add BaseStore object
1 parent d08012a commit 7d34c7d

File tree

2 files changed

+71
-14
lines changed

2 files changed

+71
-14
lines changed

lib/store.js

+61-14
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,72 @@
11
var trie = require("./trie.js");
22

3+
var NotImplemented = function (name) {
4+
return {
5+
name: "NotImplementedException",
6+
message: "method '" + name + "' not implemented"
7+
};
8+
};
9+
10+
var BaseStore = Object.create(Object.prototype, {
11+
// "abstract" methods
12+
get: { value: function (path) { throw NotImplemented("get"); } },
13+
getTarget: { value: function (path) { throw NotImplemented("getTarget"); } },
14+
getAll: { value: function (path) { throw NotImplemented("getAll"); } },
15+
add: { value: function (path) { throw NotImplemented("add"); } },
16+
update: { value: function (path) { throw NotImplemented("update"); } },
17+
remove: { value: function (path) { throw NotImplemented("remove"); } },
18+
hasRoute: { value: function (path) { throw NotImplemented("hasRoute"); } },
19+
20+
cleanPath: {
21+
value: function (path) {
22+
return trie.trim_prefix(path);
23+
}
24+
}
25+
});
26+
327
function MemoryStore () {
428
var routes = {};
529
var urls = new trie.URLTrie()
630

7-
return {
8-
get: function (path) { return routes[path]; },
9-
getTarget: function(path) { return urls.get(path); },
10-
getAll: function () { return routes; },
11-
cleanPath: function(path) { return trie.trim_prefix(path); },
12-
add: function (path, data) {
13-
routes[path] = data;
14-
urls.add(path, data);
31+
return Object.create(BaseStore, {
32+
get: {
33+
value: function (path) {
34+
return routes[path];
35+
}
1536
},
16-
update: function(path, data) { Object.assign(routes[path], data) },
17-
remove: function (path) {
18-
delete routes[path];
19-
urls.remove(path);
37+
getTarget: {
38+
value: function (path) {
39+
return urls.get(path);
40+
}
2041
},
21-
hasRoute: function (path) { return routes.hasOwnProperty(path); }
22-
};
42+
getAll: {
43+
value: function () {
44+
return routes;
45+
}
46+
},
47+
add: {
48+
value: function (path, data) {
49+
routes[path] = data;
50+
urls.add(path, data);
51+
}
52+
},
53+
update: {
54+
value: function (path, data) {
55+
Object.assign(routes[path], data);
56+
}
57+
},
58+
remove: {
59+
value: function (path) {
60+
delete routes[path];
61+
urls.remove(path);
62+
}
63+
},
64+
hasRoute: {
65+
value: function (path) {
66+
return routes.hasOwnProperty(path);
67+
}
68+
}
69+
});
2370
}
2471

2572
exports.MemoryStore = MemoryStore;

test/store_spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ describe("MemoryStore", function () {
1919
});
2020
});
2121

22+
describe("getTarget", function () {
23+
it("returns the target object for the path", function () {
24+
this.subject.add("/my_route", { "target": "http://localhost:8213" });
25+
26+
var target = this.subject.getTarget("/my_route");
27+
expect(target.prefix).toEqual("/my_route");
28+
expect(target.data.target).toEqual("http://localhost:8213");
29+
});
30+
});
31+
2232
describe("getAll", function () {
2333
it("returns all routes", function () {
2434
this.subject.add("/my_route", { "test": "value1" });

0 commit comments

Comments
 (0)