Skip to content

Commit d20d069

Browse files
committed
Conflicts: Generator.js tests/test.Generator.js Merge conflicts arising due to an earlier push and merge to the remote repository (commit 9e517c0). Solving this by: - keeping the test in tests/test.Generator.js - keeping this._online attribute for Generator (Generator.js) - ensuring use of keys in Generator (Generator.js)
2 parents 58767a6 + 6816ffa commit d20d069

6 files changed

Lines changed: 269 additions & 91 deletions

File tree

Accessor.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,34 @@ var Accessor = (function() {
2020
});
2121
};
2222

23-
Accessor.prototype.next = function(callback) {
23+
Accessor.prototype.next = function() {
24+
var key, callback;
25+
if(arguments.length === 1){
26+
if(typeof arguments[0] === 'function'){
27+
callback = arguments[0];
28+
}
29+
else if(typeof arguments[0] === 'string'){
30+
key = arguments[0];
31+
}
32+
}
33+
if(arguments.length > 1){
34+
key = arguments[0];
35+
callback = arguments[1];
36+
}
37+
2438
callback = callback || function() {};
25-
http.get(this.url + "/next", function(res) {
39+
var url = this.url + "/next" + (key ? '/'+key : '');
40+
41+
http.get(url, function(res) {
2642
var id = "";
2743
res.setEncoding("utf8");
28-
res.on("data", function(data) {id += data;});
29-
res.on("end", function() {callback(null, id);});
44+
res.on("data", function(data){id += data;});
45+
res.on("end", function(){
46+
if(id === ''){
47+
return callback(new Error("Can't generate an ID for undefined key '"+key+"'"));
48+
}
49+
callback(null, id);
50+
});
3051
}).on("error", function(err) {
3152
callback(err);
3253
});

Generator.js

Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -104,45 +104,65 @@ var Generator = (function() {
104104
function Generator(options) {
105105
options = options || {};
106106
this.options = {};
107-
this.options.digits = int(options.digits, 6);
108-
this.options.letters = int(options.letters, 3);
107+
this.keys = {};
109108
this.options.port = int(options.port, 9876);
110-
this.options.store = typeof(options.store) === "function"
109+
this.options.autoAddKeys = options.autoAddKeys ? true : false;
110+
this.server;
111+
this._started;
112+
this._stopped;
113+
this.add('__default',options);
114+
}
115+
116+
Generator.prototype.add = function(key, options){
117+
options = options || {};
118+
if(this.keys[key]){ return false; }
119+
this.keys[key] = {};
120+
this.keys[key].options = {};
121+
this.keys[key].options.digits = int(options.digits, 6);
122+
this.keys[key].options.letters = int(options.letters, 3);
123+
this.keys[key].options.store = typeof(options.store) === "function"
111124
? options.store : function() {}
112-
this.options.store_freq = int(options.store_freq, 1);
113-
this.options.restore = options.restore || null;
114-
this.numbers = -1;
115-
this.letters = "A";
125+
this.keys[key].options.store_freq = int(options.store_freq, 1);
126+
this.keys[key].options.restore = options.restore || null;
127+
this.keys[key].numbers = -1;
128+
this.keys[key].letters = "A";
116129
// workaround to get A's generated as first ids when
117130
// options.digits is 0
118131
if (options.digits === 0) {
119-
this.letters = "@";
132+
this.keys[key].letters = "@";
120133
}
121134
if (options.restore) {
122135
var result = parseId(options.restore);
123136
if (result) {
124-
this.numbers = parseInt(result.numbers);
125-
this.options.digits = result.numbers.length;
126-
this.letters = result.letters;
127-
this.options.letters = result.letters.length;
137+
this.keys[key].numbers = parseInt(result.numbers);
138+
this.keys[key].options.digits = result.numbers.length;
139+
this.keys[key].letters = result.letters;
140+
this.keys[key].options.letters = result.letters.length;
128141
}
129142
}
130-
this.generatedIds = [];
131-
this.unsavedIds = [];
132-
this.server;
133-
this._online;
134-
}
143+
this.keys[key].generatedIds = [];
144+
this.keys[key].unsavedIds = [];
135145

136-
Generator.prototype.generate = function() {
137-
var _new = generateId(this.letters, this.options.letters,
138-
this.numbers, this.options.digits);
139-
this.letters = _new.letters;
140-
this.numbers = _new.numbers;
141-
this.generatedIds.push(_new.id);
142-
this.unsavedIds.push(_new.id);
143-
if (this.options.store_freq === this.unsavedIds.length) {
144-
this.options.store(this.unsavedIds);
145-
this.unsavedIds = [];
146+
return true;
147+
};
148+
149+
Generator.prototype.generate = function(key) {
150+
if(!key){ key = '__default'; }
151+
if(!this.keys[key]){
152+
if(!this.options.autoAddKeys){
153+
return null;
154+
}
155+
this.add(key);
156+
}
157+
var _new = generateId(this.keys[key].letters, this.keys[key].options.letters,
158+
this.keys[key].numbers, this.keys[key].options.digits);
159+
this.keys[key].letters = _new.letters;
160+
this.keys[key].numbers = _new.numbers;
161+
this.keys[key].generatedIds.push(_new.id);
162+
this.keys[key].unsavedIds.push(_new.id);
163+
if (this.keys[key].options.store_freq === this.keys[key].unsavedIds.length) {
164+
this.keys[key].options.store(this.keys[key].unsavedIds);
165+
this.keys[key].unsavedIds = [];
146166
}
147167
return _new.id;
148168
};
@@ -151,26 +171,32 @@ var Generator = (function() {
151171
done = done || function() {};
152172
if (this._online) return;
153173
this.server = http.Server(function(req, res) {
154-
switch(req.url) {
155-
case "/next":
156-
res.end(this.generate());
157-
break;
158-
case "/ping":
159-
res.end("pong");
160-
break;
174+
if(req.url.match(/(^\/\w+)(?:\/(\w+))?/)){
175+
var action = RegExp.$1, key = RegExp.$2 || '__default';
176+
switch(action) {
177+
case "/next":
178+
res.end(this.generate(key));
179+
break;
180+
case "/ping":
181+
res.end("pong");
182+
break;
183+
}
161184
}
162185
}.bind(this));
163186
this.server.listen(this.options.port, done);
164187
this._online = true;
165188
};
166189

167-
Generator.prototype.store = function() {
168-
if (this.unsavedIds.length > 0) this.options.store(this.unsavedIds);
190+
Generator.prototype.store = function(key) {
191+
if(!key){ key = '__default'; }
192+
if (this.keys[key].unsavedIds.length > 0) this.keys[key].options.store(this.keys[key].unsavedIds);
169193
};
170194

171195
Generator.prototype.stop = function() {
172196
if (! this._online) return;
173-
this.store();
197+
for(var key in this.keys){
198+
this.store(key);
199+
}
174200
this.server.close();
175201
this._online = false;
176202
};

0 commit comments

Comments
 (0)