refactor key handing in storage (part 1)

This commit is contained in:
Matias Alejo Garcia 2014-09-27 11:38:25 -03:00
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); this.getGlobal('lastOpened', cb);
}; };
Storage.prototype.setFromObj = function(walletId, obj, cb) { Storage.prototype.setFromObj = function(key, obj, cb) {
preconditions.checkArgument(key);
preconditions.checkArgument(cb); preconditions.checkArgument(cb);
var self = this; this._write(key, obj, function() {
var key = 'wallet::' + walletId + ((obj.opts && obj.opts.name) ? '_' + obj.opts.name : '');
self._write(key, obj, function() {
return cb(); return cb();
}); });
}; };

View file

@ -873,7 +873,10 @@ Wallet.prototype.keepAlive = function() {
Wallet.prototype.store = function(cb) { Wallet.prototype.store = function(cb) {
var self = this; var self = this;
this.keepAlive(); 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'); log.debug('Wallet stored');
if (cb) if (cb)
cb(err); cb(err);

View file

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