all files / lib/waterline/model/lib/ model.js

100% Statements 22/22
87.5% Branches 7/8
100% Functions 2/2
100% Lines 22/22
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                                          2586×   2586× 2586×     2586×             2586×     2586×     2586× 12652×   12652× 464×         2586×                 2586×         91× 91× 91×     91×       2586×        
 
/**
 * Dependencies
 */
 
var extend = require('../../utils/extend');
var _ = require('lodash');
var util = require('util');
 
/**
 * A Basic Model Interface
 *
 * Initialize a new Model with given params
 *
 * @param {Object} attrs
 * @param {Object} options
 * @return {Object}
 * @api public
 *
 * var Person = Model.prototype;
 * var person = new Person({ name: 'Foo Bar' });
 * person.name # => 'Foo Bar'
 */
 
var Model = module.exports = function(attrs, options) {
  var self = this;
 
  attrs = attrs || {};
  options = options || {};
 
  // Store options as properties
  Object.defineProperty(this, '_properties', {
    enumerable: false,
    writable: false,
    value: options
  });
 
  // Cast things that need to be cast
  this._cast(attrs);
 
  // Build association getters and setters
  this._defineAssociations();
 
  // Attach attributes to the model instance
  for (var key in attrs) {
    this[key] = attrs[key];
 
    if (this.associationsCache.hasOwnProperty(key)) {
      this.associationsCache[key] = _.cloneDeep(attrs[key]);
    }
  }
 
  // Normalize associations
  this._normalizeAssociations();
 
 
  /**
   * Log output
   * @return {String} output when this model is util.inspect()ed
   * (usually with console.log())
   */
 
  Object.defineProperty(this, 'inspect', {
    enumerable: false,
    configurable: false,
    writable: false,
    value: function() {
      var output;
      try {
        output = self.toObject();
      } catch (e) {}
 
      return output ? util.inspect(output) : self;
    }
  });
 
  return this;
};
 
// Make Extendable
Model.extend = extend;