 serializers = {}
 
 
-_with = (name, type, tab, clazz) ->
+_with = (name, type, array, clazz) ->
   if not type then throw new Error "#{name}'s type undefined"
   if not clazz then throw new Error "#{name} undefined"
-  if tab[type]
+  if array[type]
     logger.warn "#{name} '#{type}' already defined"
   else
     logger.info "#{name} '#{type}' added"
-    tab[type] = clazz
+    array[type] = clazz
 
 withActor = (type, actor) ->
   _with 'Actor', type, actors, actor

#

_ = require "underscore"
# Factory : 
# load all dependencies
fs = require "fs"
path = require "path" 
winston = require "winston"
logger = new winston.Logger
  transports: [

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

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

loadCustom = (pathToLoad,fn) ->
  results = walkSync pathToLoad
  console.log results
  _(results).each (file) ->
    ext = path.extname(file)
    return if ext isnt ".coffee"

    # Built the relative name 
    # actors/tools/myActor.coffe 
    # => type : tools/myActor
    relative = path.relative(pathToLoad,file)
    type = relative.match(/(.+)\.coffee$/)[1]
    fn type,file 

walkSync = (dir) ->
  logger.info "Loading #{dir}"
  results = [];
  list = fs.readdirSync dir
  _(list).each (file) ->
    file = dir + '/' + file
    stat = fs.statSync(file)
    if (stat and stat.isDirectory())
      results=results.concat(walkSync(file))
    else
      results.push(file)
  return results


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