Code coverage report for node_acl/lib/memory-backend.js

Statements: 90% (45 / 50)      Branches: 85% (17 / 20)      Functions: 92.31% (12 / 13)      Lines: 90% (45 / 50)      Ignored: none     

All files » node_acl/lib/ » memory-backend.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150              1       1 1     1           42             42     42 113   42                               66       66 66                   139       139 139 139 162 69     139                   72       72 72   72 72 11   72 44   28                 19       19 19   19 19 19 25                   22       22 22 22 22 22 20           1 113     1  
/**
  Memory Backend.
	
  In-memory implementation of the storage.
*/
"use strict";
 
var 
  contract = require('./contract'),
  _ = require('lodash');
 
function MemoryBackend(){
  this._buckets = {};
};
 
MemoryBackend.prototype = {
  /**  
     Begins a transaction.
  */
  begin : function(){
    // returns a transaction object(just an array of functions will do here.)
    return [];
  },
  
  /**
     Ends a transaction (and executes it)
  */
  end : function(transaction, cb){
		contract(arguments).params('array', 'function').end();
    
    // Execute transaction
    for(var i=0, len=transaction.length;i<len;i++){
      transaction[i]();
    }
    cb();
  },
  
  /**
    Cleans the whole storage.
  */
  clean : function(cb){
    contract(arguments).params('function').end();
    this._buckets = {};
    cb();
  },
  
  /**
     Gets the contents at the bucket's key.
  */
  get : function(bucket, key, cb){
		contract(arguments)
	      .params('string', 'string', 'function')
	      .end();
    
    Eif(this._buckets[bucket]){
      cb(null, this._buckets[bucket][key] || []);
    }else{
      cb(null, []);
    }
  },
 
	/**
		Returns the union of the values in the given keys.
	*/
  union : function(bucket, keys, cb){
    contract(arguments)
  	  .params('string', 'array', 'function')
  	  .end();
    
    Eif(this._buckets[bucket]){
      var keyArrays = [];
      for(var i=0,len=keys.length;i<len;i++){
        if(this._buckets[bucket][keys[i]]){
          keyArrays.push.apply(keyArrays, this._buckets[bucket][keys[i]]);
        }
      }
      cb(undefined, _.union(keyArrays));
    }else{
      cb(undefined, []);
    }
	},
  
  /**
		Adds values to a given key inside a bucket.
	*/
	add : function(transaction, bucket, key, values){
		contract(arguments)
	      .params('array', 'string', 'string','string|array')
        .end();
        
    var self = this;
    values = makeArray(values);
    
    transaction.push(function(){
      if(!self._buckets[bucket]){
        self._buckets[bucket] = {};
      }
      if(!self._buckets[bucket][key]){
        self._buckets[bucket][key] = values;
      }else{
        self._buckets[bucket][key] = _.union(values, self._buckets[bucket][key]);
      }
    })
	},
    
  /**
     Delete the given key(s) at the bucket
  */
  del : function(transaction, bucket, keys){
		contract(arguments)
	      .params('array', 'string', 'string|array')
	      .end();
        
    var self = this;
    keys = makeArray(keys);
    
    transaction.push(function(){
      Eif(self._buckets[bucket]){
        for(var i=0, len=keys.length;i<len;i++){
          delete self._buckets[bucket][keys[i]];
        }
      }
    })
  },
  
	/**
		Removes values from a given key inside a bucket.
	*/
	remove : function(transaction, bucket, key, values){
		contract(arguments)
	      .params('array', 'string', 'string','string|array')
        .end();
        
    var self = this;
    values = makeArray(values);
    transaction.push(function(){
      var old;
      if(self._buckets[bucket] && (old = self._buckets[bucket][key])){
        self._buckets[bucket][key] = _.difference(old, values);
      }
    });
	},
}
 
function makeArray(arr){
  return Array.isArray(arr) ? arr : [arr];
}
 
exports = module.exports = MemoryBackend;