refactor key handing in storage (part 1)

This commit is contained in:
Matias Alejo Garcia 2014-09-27 11:38:25 -03:00
parent eb9ab115d7
commit 691951a86a
3 changed files with 16 additions and 15 deletions

View File

@ -330,12 +330,10 @@ Storage.prototype.getLastOpened = function(cb) {
this.getGlobal('lastOpened', cb);
};
Storage.prototype.setFromObj = function(walletId, obj, cb) {
Storage.prototype.setFromObj = function(key, obj, cb) {
preconditions.checkArgument(key);
preconditions.checkArgument(cb);
var self = this;
var key = 'wallet::' + walletId + ((obj.opts && obj.opts.name) ? '_' + obj.opts.name : '');
self._write(key, obj, function() {
this._write(key, obj, function() {
return cb();
});
};

View File

@ -873,7 +873,10 @@ Wallet.prototype.keepAlive = function() {
Wallet.prototype.store = function(cb) {
var self = this;
this.keepAlive();
this.storage.setFromObj(this.id, this.toObj(), function(err) {
var val = this.toObj();
var key = 'wallet::' + this.id + ((val.opts && val.opts.name) ? '_' + obj.opts.name : '');
this.storage.setFromObj(key, val, function(err) {
log.debug('Wallet stored');
if (cb)
cb(err);

View File

@ -177,8 +177,8 @@ describe('Storage model', function() {
var w2 = {
name: 'pepe'
};
s.setFromObj('1', w1, function() {
s.setFromObj('2', w2, function() {
s.setFromObj('wallet::1_wallet1', w1, function() {
s.setFromObj('wallet::2', w2, function() {
s.getWallets2(function(ws) {
ws[0].should.deep.equal({
id: '1',
@ -208,8 +208,8 @@ describe('Storage model', function() {
name: 'pepe'
};
s.setFromObj('1', w1, function() {
s.setFromObj('2', w2, function() {
s.setFromObj('wallet::1_wallet1', w1, function() {
s.setFromObj('wallet::2', w2, function() {
s._write('3::name', 'matias', function() {
s._write('1::name', 'juan', function() {
s.setGlobal('nameFor::3', 'wallet3', function() {
@ -280,8 +280,8 @@ describe('Storage model', function() {
name: 'pepe'
};
s.setFromObj('1', w1, function() {
s.setFromObj('2', w2, function() {
s.setFromObj('wallet::1', w1, function() {
s.setFromObj('wallet::2', w2, function() {
s.deleteWallet('3', function(err) {
err.toString().should.include('WNOTFOUND');
done();
@ -301,8 +301,8 @@ describe('Storage model', function() {
name: 'pepe'
};
s.setFromObj('1', w1, function() {
s.setFromObj('2', w2, function() {
s.setFromObj('wallet::1', w1, function() {
s.setFromObj('wallet::2', w2, function() {
s.deleteWallet('1', function(err) {
should.not.exist(err);
s.getWallets2(function(ws) {
@ -372,7 +372,7 @@ describe('Storage model', function() {
describe('#setFromObj', function() {
it('should store from an object as single key', function(done) {
s.setFromObj('id1', {
s.setFromObj('wallet::id1_nameid1', {
'key': 'val',
'opts': {
'name': 'nameid1'