Code coverage report for api/lexicon/Contexts.js

Statements: 78.79% (78 / 99)      Branches: 64.94% (50 / 77)      Functions: 82.35% (14 / 17)      Lines: 78.79% (78 / 99)      Ignored: none     

All files » api/lexicon/ » Contexts.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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 2761 1   1 2090 2090   2090 66   2024 6504     6504     2090     1             27320   27320                       588     588 588             23                 23         23     23   23 23                   23 72         72 2     23           40990           10571           10571 10571 10571 10571 10571 10571 10571 10571   10571                         1 7233 7233   7233 7233     1                                                       1231 1231     1231     1231 1231                                   21614 21614 21614 50323 44104   6219       21614 21614                                       1254     1254           1254   1254 1254 1254 1254 23 23   1231   1254             4     4 6   4     4   4                 1 1 1  
var Collection = require("./../Collection").Collection;
var FieldDBObject = require("./../FieldDBObject").FieldDBObject;
 
var Context = function Context(options) {
  Eif (!this.fieldDBtype) {
    this.fieldDBtype = "Context";
  }
  if (typeof options === "string") {
    this.morphemes = options;
  } else {
    for (var member in options) {
      Iif (!options.hasOwnProperty(member)) {
        continue;
      }
      this[member] = options[member];
    }
  }
  Object.apply(this, arguments);
};
 
Context.prototype = Object.create(Object.prototype, /** @lends Context.prototype */ {
  constructor: {
    value: Context
  },
 
  id: {
    get: function() {
      var id = this._id || this.URL || this.context || this.morphemes || this.utterance || this.orthography;
      // console.log("  id of context: " + id);
      return id;
    },
    set: function(value) {
      if (value !== this.id) {
        console.warn("setting id  on context " + value + ", it doesnt match the expected id." + this.id);
        this._id = value;
      }
    }
  },
 
  equals: {
    value: function(anotherObject) {
      Iif (!anotherObject) {
        return undefined;
      }
      Eif (this.id && this.id === anotherObject.id) {
        return true;
      }
    }
  },
 
  merge: {
    value: function(callOnSelf, anotherObject, optionalOverwriteOrAsk) {
      var anObject,
        resultObject,
        aproperty;
      // targetPropertyIsEmpty,
      // overwrite,
      // localCallOnSelf,
      // propertyList = {},
      // json;
 
      Iif (arguments.length === 0) {
        this.warn("Invalid call to merge, there was no object provided to merge");
        return null;
      }
 
      Iif (!anotherObject && !optionalOverwriteOrAsk) {
        resultObject = anObject = this;
        anotherObject = FieldDBObject.convertDocIntoItsType(callOnSelf);
      } else Eif (callOnSelf === "self") {
        // console.log("Merging properties into myself. ");
        anObject = this;
        resultObject = anObject;
      } else if (callOnSelf && anotherObject) {
        anObject = callOnSelf;
        resultObject = this;
      } else {
        this.warn("Invalid call to merge, invalid arguments were provided to merge", arguments);
        return null;
      }
      // console.log("Merging contexts", anObject, anotherObject, "into", resultObject);
 
      for (aproperty in anObject) {
        Iif (!anObject.hasOwnProperty(aproperty) ||
          typeof anObject[aproperty] === "function") {
          continue;
        }
        // console.log("merging " + aproperty);
        if (typeof anObject[aproperty] === "number") {
          resultObject[aproperty] = anObject[aproperty] + anotherObject[aproperty];
        }
      }
      return resultObject;
    }
  },
 
  isEmpty: {
    value: function() {
      return FieldDBObject.prototype.isEmpty.apply(this, arguments);
    }
  },
 
  debug: {
    value: function() {
      FieldDBObject.prototype.debug.apply(this, arguments);
    }
  },
 
  toJSON: {
    value: function(includeEvenEmptyAttributes, removeEmptyAttributes, attributesToIgnore) {
      attributesToIgnore = attributesToIgnore || [];
      attributesToIgnore = attributesToIgnore.concat(["fieldDBtype", "dateCreated"]);
      var json = FieldDBObject.prototype.toJSON.apply(this, [false, true, attributesToIgnore]);
      json.URL = json.URL || "";
      json._id = json._id || json.id;
      Eif (json._id === json.URL || json._id === json.context || json._id === json.morphemes || json._id === json.utterance || json._id === json.orthography) {
        delete json._id;
        delete json.id;
      }
      return json;
    }
  }
});
 
/**
 * @class The Contexts is a type of Collection with any additional fields or
 * metadata that a team might use to visually ground their data.
 *
 * @name  Contexts
 * @extends Collection
 * @constructs
 */
var Contexts = function Contexts(options) {
  Eif (!this._fieldDBtype) {
    this._fieldDBtype = "Contexts";
  }
  this.debug("Constructing Contexts length: ", options);
  Collection.apply(this, arguments);
};
 
Contexts.prototype = Object.create(Collection.prototype, /** @lends Contexts.prototype */ {
  constructor: {
    value: Contexts
  },
 
  api: {
    value: "contexts"
  },
 
  primaryKey: {
    value: "id"
  },
 
  INTERNAL_MODELS: {
    value: {
      item: Context
    }
  },
 
  /**
   *  Cleans a value to become a primary key on an object (replaces punctuation with underscore)
   *  (replaces the default Collection.sanitizeStringForPrimaryKey method which scrubs unicode from the primary keys)
   *
   * @param  String value the potential primary key to be cleaned
   * @return String       the value cleaned and safe as a primary key
   */
  sanitizeStringForPrimaryKey: {
    value: function(value) {
      this.debug("sanitizeStringForPrimaryKey");
      Iif (!value) {
        return null;
      }
      Iif (typeof value.replace !== "function") {
        value = value + "";
      }
      value = value.replace(/[""+=?./\[\]{}() ]/g, "");
      return value;
    }
  },
 
  // primaryKey: {
  //   get: function() {
  //     console.log(" getting primaryKey " + this._primaryKey);
 
  //     return this._primaryKey || "URL";
  //   },
  //   set: function(value) {
  //     console.log(" setting primaryKey " + value);
  //     this._primaryKey = value;
  //   }
  // },
 
  length: {
    get: function() {
      var sum = 0;
      Eif (this._collection && typeof this._collection.map === "function") {
        this._collection.map(function(context) {
          if (context.count) {
            sum += context.count;
          } else {
            sum += 1;
          }
        });
      }
      this.debug(" getting number of context lengths of " + this.id + " " + sum);
      return sum;
    },
    set: function(value) {
      this.debug(" cant set length of " + this.id + " " + value);
      // this._length = value;
    }
  },
 
  // length: {
  //   get: function() {
  //     if (this.collection) {
  //       return this.collection.length;
  //     } else {
  //       return 0;
  //     }
  //   }
  // },
 
  set: {
    value: function(searchingFor, originalValue, optionalKeyToIdentifyItem, optionalInverted) {
      Iif (!originalValue) {
        return;
      }
      if (typeof originalValue === "string") {
        // originalValue = {
        //   URL: "",
        //   context: originalValue
        // };
      }
      this.debug("context count before" + originalValue.count);
      // this.debugMode = true;
      var matches = this.find(originalValue);
      this.debug("matching contexts ", matches);
      var value;
      if (matches && matches.length) {
        value = matches[0];
        value.merge("self", originalValue);
      } else {
        value = Collection.prototype.set.apply(this, [searchingFor, originalValue, optionalKeyToIdentifyItem, optionalInverted]);
      }
      return value;
    }
  },
 
  preview: {
    configurable: true,
    get: function() {
      Iif (!this.collection) {
        return "";
      }
      var preview = this.map(function(context) {
        return context.id;
      });
      Iif (!preview && this.length) {
        preview = this.length + " contexts";
      } else {
        preview = preview.join("; ");
      }
      return preview;
    },
    set: function() {
      //cant set preview
    }
  }
 
});
 
exports.Context = Context;
exports.Contexts = Contexts;
exports.Contexts.Context = Contexts.Context;