update File to write wallets to different files

the walletId is the filename
This commit is contained in:
Ryan X. Charles 2014-04-16 17:37:32 -03:00
parent b442e110e4
commit 5f8deb7d0b
2 changed files with 51 additions and 43 deletions

View File

@ -6,47 +6,54 @@ function Storage(opts) {
opts = opts || {}; opts = opts || {};
this.data = {}; this.data = {};
this.filename = opts.filename;
} }
Storage.prototype.load = function(callback) { Storage.prototype.load = function(walletId, callback) {
if (!this.filename) fs.readFile(walletId, function(err, data) {
throw new Error('No filename');
fs.readFile(this.filename, function(err, data) {
if (err) return callback(err); if (err) return callback(err);
try { try {
this.data = JSON.parse(data); this.data[walletId] = JSON.parse(data);
} catch (err) { } catch (err) {
return callback(err); if (callback)
return callback(err);
} }
return callback(null); if (callback)
return callback(null);
}); });
}; };
Storage.prototype.save = function(callback) { Storage.prototype.save = function(walletId, callback) {
var data = JSON.stringify(this.data); var data = JSON.stringify(this.data[walletId]);
//TODO: update to use a queue to ensure that saves are made sequentially //TODO: update to use a queue to ensure that saves are made sequentially
fs.writeFile(this.filename, data, function(err) { fs.writeFile(walletId, data, function(err) {
return callback(err); if (callback)
return callback(err);
}); });
}; };
Storage.prototype._read = function(k) { Storage.prototype._read = function(k) {
return this.data[k]; var split = k.split('::');
var walletId = split[0];
var key = split[1];
return this.data[walletId][key];
}; };
Storage.prototype._write = function(k, v, callback) { Storage.prototype._write = function(k, v, callback) {
this.data[k] = v; var split = k.split('::');
this.save(callback); var walletId = split[0];
var key = split[1];
if (!this.data[walletId])
this.data[walletId] = {};
this.data[walletId][key] = v;
this.save(walletId, callback);
}; };
// get value by key // get value by key
Storage.prototype.getGlobal = function(k) { Storage.prototype.getGlobal = function(k) {
return this.data[k]; return this._read(k);
}; };
// set value for key // set value for key
@ -56,13 +63,17 @@ Storage.prototype.setGlobal = function(k, v, callback) {
// remove value for key // remove value for key
Storage.prototype.removeGlobal = function(k, callback) { Storage.prototype.removeGlobal = function(k, callback) {
delete this.data[k]; var split = k.split('::');
this.save(callback); var walletId = split[0];
var key = split[1];
delete this.data[walletId][key];
this.save(walletId, callback);
}; };
Storage.prototype._key = function(walletId, k) { Storage.prototype._key = function(walletId, k) {
return walletId + '::' + k; return walletId + '::' + k;
}; };
// get value by key // get value by key
Storage.prototype.get = function(walletId, k) { Storage.prototype.get = function(walletId, k) {
return this.getGlobal(this._key(walletId, k)); return this.getGlobal(this._key(walletId, k));

View File

@ -18,8 +18,8 @@ describe('Storage/File', function() {
callback(); callback();
}; };
var Storage = require('soop').load('../js/models/storage/File.js', {fs: fs}); var Storage = require('soop').load('../js/models/storage/File.js', {fs: fs});
var storage = new Storage({filename: 'myfilename', password: 'password'}); var storage = new Storage({password: 'password'});
storage.load(function(err) { storage.load('myfilename', function(err) {
done(); done();
}); });
}); });
@ -33,8 +33,8 @@ describe('Storage/File', function() {
callback(); callback();
}; };
var Storage = require('soop').load('../js/models/storage/File.js', {fs: fs}); var Storage = require('soop').load('../js/models/storage/File.js', {fs: fs});
var storage = new Storage({filename: 'myfilename', password: 'password'}); var storage = new Storage({password: 'password'});
storage.save(function(err) { storage.save('myfilename', function(err) {
done(); done();
}); });
}); });
@ -43,45 +43,42 @@ describe('Storage/File', function() {
describe('#_read', function() { describe('#_read', function() {
it('should return the value of a key', function() { it('should return the value of a key', function() {
var storage = new Storage(); var storage = new Storage();
storage.data = {'test':'data'}; storage.data = {'walletId':{'test':'data'}};
storage._read('test').should.equal('data'); storage._read('walletId::test').should.equal('data');
}); });
}); });
describe('#_write', function() { describe('#_write', function() {
it('should save the value of a key and then run save', function(done) { it('should save the value of a key and then run save', function(done) {
var storage = new Storage(); var storage = new Storage();
storage.save = function(callback) { storage.save = function(walletId, callback) {
storage.data['key'].should.equal('value'); storage.data[walletId]['key'].should.equal('value');
callback(); callback();
}; };
storage._write('key', 'value', function() { storage._write('walletId::key', 'value', function() {
done(); done();
}); });
}); });
}); });
describe('#getGlobal', function() { describe('#getGlobal', function() {
it('should store a global key', function(done) { it('should call storage._read', function() {
var storage = new Storage(); var storage = new Storage();
storage.save = function(callback) { storage.data = {'walletId':{'test':'test'}};
storage.data['key'].should.equal('value'); storage._read = sinon.spy();
callback(); storage.getGlobal('walletId::test');
}; storage._read.calledOnce.should.equal(true);
storage.setGlobal('key', 'value', function() {
done();
});
}); });
}); });
describe('#setGlobal', function() { describe('#setGlobal', function() {
it('should store a global key', function(done) { it('should store a global key', function(done) {
var storage = new Storage(); var storage = new Storage();
storage.save = function(callback) { storage.save = function(walletId, callback) {
storage.data['key'].should.equal('value'); storage.data[walletId]['key'].should.equal('value');
callback(); callback();
}; };
storage.setGlobal('key', 'value', function() { storage.setGlobal('walletId::key', 'value', function() {
done(); done();
}); });
}); });
@ -90,12 +87,12 @@ describe('Storage/File', function() {
describe('#removeGlobal', function() { describe('#removeGlobal', function() {
it('should remove a global key', function(done) { it('should remove a global key', function(done) {
var storage = new Storage(); var storage = new Storage();
storage.data.key = 'value'; storage.data = {'walletId':{'key':'value'}};
storage.save = function(callback) { storage.save = function(walletId, callback) {
should.not.exist(storage.data['key']); should.not.exist(storage.data[walletId]['key']);
callback(); callback();
}; };
storage.removeGlobal('key', function() { storage.removeGlobal('walletId::key', function() {
done(); done();
}); });
}); });