Permalink
Browse files

[lib] Fixed the ability to reconstitute a vat from a JSON object

  • Loading branch information...
1 parent 79c9dec commit 6c61f269099623f14572710ce92bd626a01c30ac Paolo Fragomeni committed Mar 22, 2012
Showing with 45 additions and 16 deletions.
  1. +22 −14 lib/eventvat.js
  2. +1 −1 package.json
  3. +22 −1 test/methods-test.js
View
@@ -29,7 +29,15 @@
return new EventVat(conf);
}
- this.hash = conf && conf.data || {};
+ var that = this;
+ this.hash = {};
+
+ if (conf && conf.data) {
+
+ Object.keys(conf.data).forEach(function(key) {
+ that.hash[key] = { value: conf.data[key] };
+ });
+ }
};
var p = EventVat.prototype;
@@ -58,8 +66,8 @@
//
// Delete a key
//
- p.del = function(key /* ... */) {
- var n = 0;
+ p.del = function() {
+ var n = 0, key;
for (var i = 0, l = arguments.length; i < l; i++) {
key = arguments[i];
@@ -161,7 +169,7 @@
//
// Inspect the internals of EventVat objects
//
- p.object = function(subcommend /* ... */) {
+ p.object = function(subcommend) {
throw new Error('Not implemented.');
};
@@ -422,7 +430,7 @@
//
// Get the values of all the given keys
//
- p.mget = function(key /* ... */) {
+ p.mget = function() {
var values = [];
for(var i=0, l=arguments.length; i < l; i++) {
values.push(this.get(arguments[i]));
@@ -434,7 +442,7 @@
//
// Set multiple keys to multiple values
//
- p.mset = function(keys /* ... */, values /* ... */) {
+ p.mset = function() {
var key, value;
for(var i=0, l=arguments.length; i < l; i += 2) {
@@ -468,7 +476,7 @@
//
// Set multiple keys to multiple values, only if none of the keys exist
//
- p.msetnx = function(keys /* ... */, values /* ... */) {
+ p.msetnx = function() {
var key, value;
for(var i=0, l=arguments.length; i < l; i += 2) {
@@ -596,8 +604,8 @@
//
// delete one or more hash fields
//
- p.hdel = function(key, field /* ... */) {
- var n = 0;
+ p.hdel = function(key) {
+ var n = 0, field;
if (this.type(key) === 'hash') {
this.persist(key);
@@ -770,7 +778,7 @@
//
// get the values of all the given hash fields
//
- p.hmget = function(key, field /* ... */) {
+ p.hmget = function(key, field) {
var values = [];
for(var i=1, l=arguments.length; i < l; i++) {
@@ -785,7 +793,7 @@
//
// set multiple hash fields to multiple values
//
- p.hmset = function(key, fields /* ... */, values /* ... */) {
+ p.hmset = function(key) {
var type = this.type(key);
if (type === 'none') {
this.hash[key] = { value: {}, type: 'hash' };
@@ -841,7 +849,7 @@
//
// Set the value of a hash field, only if the field does not exist
//
- p.hsetnx = function(key, field, value /* ... */) {
+ p.hsetnx = function(key, field, value) {
var type = this.type(key);
if (type === 'none') {
this.hash[key] = { value: {}, type: 'hash' };
@@ -950,7 +958,7 @@
//
// Prepend one or multiple values to a list
//
- p.lpush = function(key, value /* ... */) {
+ p.lpush = function(key) {
var type = this.type(key);
var list;
if (type === 'none') {
@@ -1140,7 +1148,7 @@
//
// Append one or multiple values to a list
//
- p.rpush = function(key, value /* ... */ ) {
+ p.rpush = function(key) {
var type = this.type(key);
var list;
if (type === 'none') {
View
@@ -1,6 +1,6 @@
{
"name" : "eventvat",
- "version" : "0.1.8",
+ "version" : "0.1.9",
"description" : "Evented, in-process key/value store for Node.js and the browser",
"main" : "./lib/eventvat.js",
"directories" : {
View
@@ -957,7 +957,7 @@ this.methodSuite = {
var vat = EventVat();
- vat.rpush('list', 'one')
+ vat.rpush('list', 'one');
vat.rpush('list', 'two');
vat.rpush('list', 'three');
vat.rpush('list', 'four');
@@ -971,4 +971,25 @@ this.methodSuite = {
test.done();
},
+ 'Reconstitute a vat from a JSON object': function(test) {
+
+ var vat1 = EventVat();
+
+ vat1.set('foo', 'a', 1);
+ vat1.hset('bar', 'a', 1);
+
+ vat1.rpush('bazz', 'a');
+ vat1.rpush('bazz', 'b');
+
+ var vat2 = EventVat({
+ data: vat1.dump()
+ });
+
+ test.equal(vat2.get('foo'), 'a');
+ test.equal(vat2.llen('bazz'), 2);
+ test.equal(vat2.hget('bar', 'a'), 1);
+
+ test.done();
+
+ },
};

0 comments on commit 6c61f26

Please sign in to comment.