Code coverage report for src/path.js

Statements: 86.57% (58 / 67)      Branches: 65% (13 / 20)      Functions: 62.5% (5 / 8)      Lines: 87.88% (58 / 66)      Ignored: none     

All files » src/ » path.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 151 152 153    1   8     8           8 8       8 2 2 2       2       2     8 8 8 8 8 8 8 8 8 8 8 8   8                                     8     2 2 2 2       2 2 6 6 3             2   2 2 4 4 2             2   2         2         2 2 2     2 2     8 1     8 1                     8   8       8   8     1 1          
// # Path
// Path Object represents a path between two Nodes
var __initPath__ = function(neo4jrestful) {
 
  var helpers = null
    , _       = null
 
  Iif (typeof window === 'object') {
    // browser
    helpers = window.Neo4jMapper.helpers;
    _       = window._;
  } else {
    // nodejs
    helpers = require('./helpers')
    _       = require('underscore');
  }
 
  // Constructor of Path
  var Path = function Path() {
    this.nodes = [];
    this.relationships = [];
    this.from = {
      id: null,
      uri: null
    };
    this.to = {
      id: null,
      uri: null
    };
    this._is_instanced_ = true;
  }
 
  Path.prototype.classification   = 'Path';   // only needed for toObject()
  Path.prototype.from             = null;
  Path.prototype.to               = null;
  Path.prototype.start            = null;
  Path.prototype.end              = null;
  Path.prototype.length           = 0;
  Path.prototype.relationships    = null;
  Path.prototype.nodes            = null;
  Path.prototype._response_       = null;
  Path.prototype._is_singleton_   = false;
  Path.prototype._is_persisted_   = false;
  Path.prototype._is_instanced_   = null;
 
  Path.prototype.singleton = function() {
    var path = new Path();
    path._is_singleton_ = true;
    return path;
  }
 
  /*
  [
    { start: 'http://localhost:7419/db/data/node/1019',
      nodes:
       [ 'http://localhost:7419/db/data/node/1019',
         'http://localhost:7419/db/data/node/1020',
         'http://localhost:7419/db/data/node/1021' ],
      length: 2,
      relationships:
       [ 'http://localhost:7419/db/data/relationship/315',
         'http://localhost:7419/db/data/relationship/316' ],
      end: 'http://localhost:7419/db/data/node/1021' } ]
  */
  Path.prototype.populateWithDataFromResponse = function(data) {
    // if we are working on the prototype object
    // we won't mutate it and create a new path instance insetad
    var path = (this._is_instanced_ !== null) ? this : new Path();
    Eif (data) {
      Eif (_.isObject(data) && (!_.isArray(data)))
        path._response_ = data;
      else
        path._response_ = data[0];
 
      Eif (_.isArray(data.nodes)) {
        for (var i=0; i < data.nodes.length; i++) {
          var url = data.nodes[i];
          if (/[0-9]+$/.test(url))
            data.nodes[i] = {
              uri: url,
              id: Number(url.match(/[0-9]+$/)[0])
            }
        }
      }
 
      path.nodes = _.extend(data.nodes);
 
      Eif (_.isArray(data.relationships)) {
        for (var i=0; i < data.relationships.length; i++) {
          var url = data.relationships[i];
          if (/[0-9]+$/.test(url))
            data.relationships[i] = {
              uri: url,
              id: Number(url.match(/[0-9]+$/)[0])
            }
        }
      }
 
      path.relationships = _.extend(data.relationships);
 
      path.from = {
        id: Number(data.start.match(/[0-9]+$/)[0]),
        uri: data.start
      }
 
      path.to = {
        id: Number(data.end.match(/[0-9]+$/)[0]),
        uri: data.end
      }
 
      path.start = data.start;
      path.end = data.end;
      path.length = data.length;
 
    }
    path._is_persisted_ = true;
    return path;
  }
 
  Path.prototype.load = function(cb) {
    cb(null, this);
  }
 
  Path.prototype.toObject = function() {
    return {
      classification: this.classification,
      start: this.start,
      end: this.end,
      from: _.extend(this.from),
      to: _.extend(this.to),
      relationships: _.extend(this.relationships),
      nodes: _.extend(this.nodes)
    };
  }
 
  Path.prototype.resetQuery = function() { return this; }
 
  Path.new = function() {
    return new Path();
  }
 
  Path.create = Path.new;
 
  return neo4jrestful.Path = Path;
}
 
Eif (typeof window !== 'object') {
  module.exports = exports = {
    init: __initPath__
  };
} else {
  window.Neo4jMapper.initPath = __initPath__;
}