Code coverage report for lib/util.js

Statements: 100% (31 / 31)      Branches: 100% (6 / 6)      Functions: 100% (9 / 9)      Lines: 100% (31 / 31)      Ignored: none     

All files » lib/ » util.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 1021                   1 14 14 14   14 14 14 14                   1 19 5   14                 1 11               1 2 2               1 1   1 1               1 1               1 2               1 5           1 4     1
var log4js = require('log4js');
 
/**
 * class Util
 * Util is exposed to couchtato.js task functions.
 *
 * @param {Object} stat: initial stat, used to initialise reportable page and document counts to zero
 * @param {Array} queue: an array of documents waiting to be updated in CouchDB
 * @param {Object} driver: the database driver used by Couchtato, exposed via Util to allow further database operation from task functions
 */
function Util(stat, queue, driver) {
  this.stat = stat || {};
  this.queue = queue || [];
  this.driver = driver;
 
  log4js.loadAppender('file');
  log4js.addAppender(log4js.appenders.file('couchtato.log'), '');
  this.logger = log4js.getLogger('');
  this.logger.setLevel('INFO');
}
 
/**
 * Increment stat count for existing key.
 * For new key, stat count will be set to increment value.
 *
 * @param {String} key: stat key
 * @param {Number} increment: increment value
 */
Util.prototype.increment = function (key, increment) {
  if (this.stat[key]) {
    this.stat[key] += increment;
  } else {
    this.stat[key] = increment;
  } 
};
 
/**
 * Increment stat count by 1.
 *
 * @param {String} key: stat key
 */
Util.prototype.count = function (key) {
  this.increment(key, 1);
};
 
/**
 * Queue document for saving, increment save counter.
 *
 * @param {Object} doc: CouchDB document
 */
Util.prototype.save = function (doc) {
  this.count('_couchtato_save');
  this.queue.push(doc);
};
 
/**
 * Mark and queue document for deletion, increment delete counter.
 *
 * @param {Object} doc: CouchDB document
 */
Util.prototype.remove = function (doc) {
  this.count('_couchtato_remove');
 
  doc._deleted = true;
  this.queue.push(doc);
};
 
/**
 * Log message in file.
 *
 * @param {String} message: the message to log
 */
Util.prototype.log = function (message) {
  this.logger.info(message);
};
 
/**
 * Get stat object containing counts.
 *
 * @return stat object
 */
Util.prototype.getStat = function () {
  return this.stat;
};
 
/**
 * Get queue containing docs to be updated. 
 *
 * @return queue
 */
Util.prototype.getQueue = function () {
  return this.queue;
};
 
/**
 * Empty the queue.
 */
Util.prototype.resetQueue = function () {
  this.queue = [];
};
 
module.exports = Util;