Code coverage report for father/lib/package.js

Statements: 100% (37 / 37)      Branches: 87.5% (14 / 16)      Functions: 81.82% (9 / 11)      Lines: 100% (37 / 37)      Ignored: none     

All files » father/lib/ » package.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    1   1   9 7   2     9 9 9       9 9   9   9   10 10 7   10     9 9       11 4     7 7         129 58     71       9 9 45     40 4 4 9 9   4   36 36                               1  
'use strict';
 
var Class = require('arale').Class;
 
var Package = Class.create({
  initialize: function(dir, father) {
    if (father) {
      this.father = father;
    } else {
      this.packages = {};
    }
 
    this.dest = dir;
    this._dependencies = [];
    this.run();
  },
 
  run: function() {
    var that = this, Self = this.constructor;
    var pkg = this._pkg = this.readPackage();
 
    this.id = pkg.id;
 
    Object.keys(pkg.dependencies)
      .forEach(function(name) {
        var sub = pkg.dependencies[name];
        if (!that.get(sub.id)) {
          that.set(new Self(sub.dest, that));
        }
        this._dependencies.push(sub.id);
      }.bind(this));
 
    var keys = ['name', 'version', 'main', 'origin', 'dependencies'];
    this.exportProperty(keys);
  },
 
  set: function(pkg) {
    if (this.father) {
      return this.father.set(pkg);
    }
 
    Eif (!this.packages[pkg.id]) {
      this.packages[pkg.id] = pkg;
    }
  },
 
  get: function(id) {
    if (this.father) {
      return this.father.get(id);
    }
 
    return id === this.id ? this : this.packages[id];
  },
 
  exportProperty: function(keys) {
    var that = this;
    keys.forEach(function(key) {
      Object.defineProperty(that, key, {
        set: function() {},
        get: function() {
          if (key === 'dependencies') {
            var deps = {};
            that._dependencies.forEach(function(id) {
              var pkg = that.get(id);
              deps[pkg.name] = pkg;
            });
            return deps;
          } else {
            var pkg = that.get(that.id);
            return pkg ? pkg._pkg[key] : '';
          }
        },
        configurable: true
      });
    });
  },
 
  /*
    Method below can be overridden
  */
 
  readPackage: function() {}
 
});
 
module.exports = Package;