var Toposort   = require('toposort-class')
  , DaoFactory = require('./dao-factory')

module.exports = (function() {
  var DAOFactoryManager = function(sequelize) {
    this.daos = []
    this.sequelize = sequelize
  }

  DAOFactoryManager.prototype.addDAO = function(dao) {
    this.daos.push(dao)

    return dao
  }

  DAOFactoryManager.prototype.removeDAO = function(dao) {
    this.daos = this.daos.filter(function(_dao) {
      return _dao.name != dao.name
    })
  }

  DAOFactoryManager.prototype.getDAO = function(daoName, options) {
    options = options || {}
    options.attribute = options.attribute || 'name'

    var dao = this.daos.filter(function(dao) {
      return dao[options.attribute] === daoName
    })

    return !!dao ? dao[0] : null
  }

  DAOFactoryManager.prototype.__defineGetter__('all', function() {
    return this.daos
  })

  /**
   * Iterate over DAOs in an order suitable for e.g. creating tables. Will
   * take foreign key constraints into account so that dependencies are visited
   * before dependents.
   */
  DAOFactoryManager.prototype.forEachDAO = function(iterator) {
    var daos   = {}
      , sorter = new Toposort()

    this.daos.forEach(function(dao) {
      var deps = []

      daos[dao.tableName] = dao

      for (var attrName in dao.rawAttributes) {
        if (dao.rawAttributes.hasOwnProperty(attrName)) {
          (function(reference) {
            if (!!reference) {
              deps.push((reference instanceof DaoFactory) ? reference.tableName : reference)
            }
          })(dao.rawAttributes[attrName].references)
        }
      }

      sorter.add(dao.tableName, deps)
    })

    sorter.sort().reverse().forEach(function(name) {
      iterator(daos[name])
    })
  }

  return DAOFactoryManager
})()

@@ -1372,144 +1372,40 @@
     
-(function(reference) %7B%0A            if (!!reference) %7B%0A              deps.push((reference instanceof DaoFactory) ? reference.table
+if (dao.rawAttributes%5Battr
 Name
- : 
+%5D.
 refe
@@ -1409,17 +1409,20 @@
 eference
-)
+s) %7B
 %0A       
@@ -1430,22 +1430,17 @@
     
-%7D%0A          %7D)
+deps.push
 (dao
@@ -1476,16 +1476,28 @@
 rences)%0A
+          %7D%0A
         

reverted prev. change