Code coverage report for upstream/lib/associations/mixin.js

Statements: 100% (45 / 45)      Branches: 75% (21 / 28)      Functions: 83.33% (5 / 6)      Lines: 100% (45 / 45)     

All files » upstream/lib/associations/ » mixin.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 941           1   1   25 25 25     25 25   25 25   25     1   41 41 41     41 41   41 41   41     1   108 108 108     108 108   108 108   108     1 26   26 29 29   29 22         26     1 10   10 14 14   14 8         10                
var Utils     = require("./../utils")
  , HasOne    = require('./has-one')
  , HasMany   = require("./has-many")
  , BelongsTo = require("./belongs-to")
 
/* Defines Mixin for all models. */
var Mixin = module.exports = function(){}
 
Mixin.hasOne = function(associatedDAOFactory, options) {
  // Since this is a mixin, we'll need a unique variable name for hooks (since DAOFactory will override our hooks option)
  options = options || {}
  options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
  options.useHooks = options.hooks
 
  // the id is in the foreign table
  var association = new HasOne(this, associatedDAOFactory, Utils._.extend((options||{}), this.options))
  this.associations[association.associationAccessor] = association.injectAttributes()
 
  association.injectGetter(this.DAO.prototype);
  association.injectSetter(this.DAO.prototype);
 
  return this
}
 
Mixin.belongsTo = function(associatedDAOFactory, options) {
  // Since this is a mixin, we'll need a unique variable name for hooks (since DAOFactory will override our hooks option)
  options = options || {}
  options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
  options.useHooks = options.hooks
 
  // the id is in this table
  var association = new BelongsTo(this, associatedDAOFactory, Utils._.extend(options, this.options))
  this.associations[association.associationAccessor] = association.injectAttributes()
 
  association.injectGetter(this.DAO.prototype)
  association.injectSetter(this.DAO.prototype)
 
  return this
}
 
Mixin.hasMany = function(associatedDAOFactory, options) {
  // Since this is a mixin, we'll need a unique variable name for hooks (since DAOFactory will override our hooks option)
  options = options || {}
  options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
  options.useHooks = options.hooks
 
  // the id is in the foreign table or in a connecting table
  var association = new HasMany(this, associatedDAOFactory, Utils._.extend((options||{}), this.options))
  this.associations[association.associationAccessor] = association.injectAttributes()
 
  association.injectGetter(this.DAO.prototype)
  association.injectSetter(this.DAO.prototype)
 
  return this
}
 
Mixin.getAssociation = function(target) {
  var result = null
 
  for (var associationName in this.associations) {
    Eif (this.associations.hasOwnProperty(associationName)) {
      var association = this.associations[associationName]
 
      if (!result && (association.target === target)) {
        result = association
      }
    }
  }
 
  return result
}
 
Mixin.getAssociationByAlias = function(alias) {
  var result = null
 
  for (var associationName in this.associations) {
    Eif (this.associations.hasOwnProperty(associationName)) {
      var association = this.associations[associationName]
 
      if (!result && (association.options.as === alias)) {
        result = association
      }
    }
  }
 
  return result
}
 
/* example for instance methods:
  Mixin.prototype.test = function() {
    console.log('asd')
  }
*/