 loadCustom "#{process.cwd()}/adapters", withAdapter
 loadCustom "#{process.cwd()}/serializers", withSerializer
 
-_new = (type, tab, properties) ->
+_new = (type, array, properties) ->
   throw new Error "type undefined" if not type
-  if not tab[type]
-    tab[type] = require type
+  if not array[type]
+    # can require a type into external module
+    array[type] = require type
   else 
-    if typeof tab[type] is "string"   
-      tab[type] = require tab[type]
-  new tab[type] properties
+    if typeof array[type] is "string"   
+      array[type] = require array[type]
+  new array[type] properties
 
 newActor = (type, properties) ->
   _new type, actors, properties

withSerializer = (type, serializer) ->
  _with 'Serializer', type, serializers, serializer

loadBuiltin  = (builtinNames,tab,type) ->  
  _.pairs(builtinNames).forEach (pair) ->
    name = pair[1]
    tab[name]=require "./#{type}/#{name}"



loadCustom = (pathToLoad,fn) ->
  walk pathToLoad, (err,results) ->
    throw err if err
    _(results).each (file) ->
      ext = path.extname(file)
      if ext is ".js" or ext is ".node" or ext is ".json"
        # TODO : Error or Warning 
        logger.warn "File #{file} will override the .coffee version" 
      return if path.extname(file) isnt ".coffee"
      fn path.basename(file,".coffee"),file 

walk = (dir, done) -> 
  logger.info "Loading #{dir}"
  results = [];
  fs.readdir dir, (err, list) ->
    return done(err) if err
    i = 0;
    next = () ->
      file = list[i++];
      return done(null, results) if not file
      file = dir + '/' + file;
      fs.stat file, (err, stat) ->
        if (stat and stat.isDirectory())
          walk file, (err, res) ->
            results = results.concat(res);
            next();
        else
          results.push(file);
          next();
    next()

loadBuiltin builtinActorNames,actors,"actor"
loadBuiltin builtinAdapterNames,adapters,"adapters"

#

_ = require "underscore"

# Factory : 
# load all dependencies
fs = require "fs"
path = require "path" 

winston = require "winston"
logger = new winston.Logger
  transports: [
