Use fire to identify which madul is firing these events

    q     = require 'q'
    fs    = require 'fs'
    async = require 'async'
    uuid  = require 'uuid-1345'

    microtime = require 'microtime'

    EventEmitter2 = require('eventemitter2').EventEmitter2
    emitter       = new EventEmitter2 wildcard: true

    available   = { }
    initialized = { }
    listeners   = { }

    underscore = (value) -> value.replace /\W+/g, '_'
    strip      = (name)  -> name.toLowerCase().replace /\W+/g, ''

    SEARCH_ROOTS  = { }
    WRAPPED       = { }
    _INITIALIZERS = { }
    HYDRATED      = { }
    LOCALS        = { }

    SKIP_WRAP = [
      'constructor'
      'initialize'
      'details'
      'fire'
      'warn'
      'listen'
      'set'
      'runtime_exception'
    ]

    HYRDATION_LISTENERS = { }

    INITIALIZERS = (cls, method) ->
      _INITIALIZERS[cls] = [ ] unless _INITIALIZERS[cls]?

      _INITIALIZERS[cls].push fn: method, called: false

    WRAP = (cls, method) ->
      WRAPPED[cls] = [ ] unless WRAPPED[cls]?

      WRAPPED[cls].indexOf(method) < 0

    determine_local = (token) ->
      project_local = false
      node_local    = false
      name          = token.substring 1, token.length

      if token[0] == '.'
        project_local = true
      else if token[0] == '~'
        node_local = true
      else
        name = token

      [project_local, node_local, name]

    parse_name_and_local = (tokens) ->
      [project_local, node_local, name] = determine_local tokens[0].trim()

      if tokens.length == 2
        alias = tokens[1].trim()
      else if tokens.length == 1
        alias = name
      else
        return Madul.FIRE '!.Madul.dependency_spec.alias.invalid', tokens[1]

      [project_local, node_local, name, alias]

    parse_alias_and_parent = (token) ->
      t = token.split '->'

      if t[0].indexOf('#') > -1
        r      = t[0].split '#'
        parent = r[0].trim()
        t[0]   = r[1]

      [parent, t]

    extract_properties = (token) ->
      [parent, t] = parse_alias_and_parent token
      [project_local, node_local, name, alias] = parse_name_and_local t

      [project_local, node_local, name, alias, parent]

    class Madul

      details: =>
        name  = @.__proto__.constructor.name
        props = for own key, val of @.__proto__
          if key != 'constructor' &&
             key != 'details'     &&
             key != 'initialize'  && ((
               key[0]            == '_' &&
               @.__proto__[key]? == false
             ) || key[0] != '_')

            if typeof val == 'function'
              value = val.constructor.name
            else if Array.isArray val
              value = for v in val
                if typeof v == 'function'
                  v.constructor.name
                else if typeof v == 'object'
                  v.constructor.name
                else
                  v
              value = value.join ','
            else if typeof val == 'object'
              value = val.constructor.name
            else
              value = val

            "#{key} => #{value}"

        "\n  #{name}\n    #{props.filter((e) -> e?).join '\n    '}"

      @LISTEN: (event, callback) => emitter.on event, callback

      @FIRE: (event, args) => emitter.emit event, args

      @SEARCH_ROOT: (mod, path) =>
        Madul.FIRE '$.Madul.search_root.register', "#{mod}": path

        SEARCH_ROOTS[mod] = path.substring 0, path.lastIndexOf '/'

      @PARSE_SPEC: (value) =>
        tokens = value.split '='

        [project_local, node_local, name, alias, parent] = extract_properties tokens[0]

        if tokens.length == 2
          initializer = tokens[1].trim()
          toks        = initializer.split ':'
          initializer = toks[0].trim()

          if toks.length == 2
            prerequisites = toks[1].split(',').map (p) => p.trim()
          else if toks.length > 2
            return Madul.FIRE '!.Madul.dependency_spec.initializer.invalid', tokens[1]

        else if tokens.length > 2
          return Madul.FIRE '!.Madul.dependency_spec.invalid', d

        { ref: alias || name, alias, name, parent, initializer, prerequisites, project_local, node_local }

      @BUILD_SPEC: ({ search_root, name, alias, initializer, prerequisites }) =>
        unless name?
          return Madul.FIRE '!.Madul.dependency_spec.name-required', arguments[0]

        if prerequisites? && initializer? == false
          return Madul.FIRE '!.Madul.dependency_spec.prerequisites-require-initializer', arguments[0]

        "#{if search_root?   then "#{search_root}#"            else ''}\
         #{name}\
         #{if alias?         then " -> #{alias}"               else ''}\
         #{if initializer?   then " = #{initializer}"          else ''}\
         #{if prerequisites? then ":#{prerequisites.join ','}" else ''}\
        "

      listen: (event, callback) ->
        Madul.LISTEN "*.#{@constructor.name}.#{event}", callback

      fire: (event, args) ->
        Madul.FIRE "@.#{@constructor.name}.#{event}", args

      warn: (event, details, finisher) =>
        Madul.FIRE "!.#{@constructor.name}.#{event}", details

        finisher() if typeof finisher == 'function'

      runtime_exception: (exit = false) =>
        (err) =>
          Madul.FIRE "!.#{@constructor.name}.runtime-exception",
            stack:     err.stack
            message:   err.message
            timestamp: microtime.now()

          process.exit(1) if exit == 'DIE_ON_EXCEPTION'

      set: (prop, val) =>
        proto = @.__proto__
        mod   = available[strip proto.constructor.name.toLowerCase()]

        if mod?
          mod.__proto__[prop] = val

      _report: (method, state, id, args, breaker) =>
        invoke = undefined
        params =
          uuid:      id
          args:      args
          timestamp: microtime.now()

        if state == 'reject'
          if typeof breaker == 'function'
            stop = new Error()
            stop.break = true

            breaker stop

          @warn "#{method}.#{state}", params
        else
          @fire "#{method}.#{state}", params

      _respond: (proto, method, id, deferred) =>
        (state, data, breaker) =>
          process.nextTick =>
            deferred[state] data

            proto._report method, state, id, data, breaker

      _prep_invocation: (proto, method, input, def) =>
        id  = uuid.v4fast()
        res = proto._respond proto, method, id, def
        e   = (brk) =>
          error = (err) => res 'reject', err, brk

          if typeof brk == 'function'
            error
          else
            error brk

        if Array.isArray input
          input.push (out) => res 'resolve', out
          input.push e
          input.push (out) => res 'notify',  out
        else if typeof input == 'object'
          input.done   = (out) => res 'resolve', out
          input.fail   = e
          input.update = (out) => res 'notify',  out

          input = [ input ]
        else
          msg = "input to #{proto.constructor.name}.#{method} must be an Array of Object"

          proto.warn.call proto, 'input.invalid', msg

          throw new Error msg

        proto._report method, 'invoke', id, input

        input

      _process_decorators: (proto, type) =>
        if Array.isArray proto[type]
          proto[type].map (b) => Madul.PARSE_SPEC(b).ref
        else if typeof proto[type] == 'string'
          [ Madul.PARSE_SPEC(proto[type]).ref ]
        else
          [ ]

      _do_wrap: (proto, method, wrapped) =>
        if SKIP_WRAP.includes method
          wrapped()
        else
          Madul.FIRE "$.#{proto.constructor.name}.#{method}.wrap"

          if typeof proto[method] == 'function'
            proto["_#{method}"] = proto[method]

            proto[method] = =>
              def   = q.defer()
              args  = Array.prototype.slice.call arguments
              input = proto._prep_invocation proto, method, args, def

              try
                proto["_#{method}"].apply proto, input
              catch e
                failed = if input[0].fail? then input[0].fail else input[input.length - 2]

                proto.warn.call proto, 'runtime-exception', stack: e.stack, message: e.message

                failed e.message

              def.promise

            INITIALIZERS proto.constructor.name, method if method[0] == '$'

            wrapped()
          else if typeof proto[method] == 'object'

            if Array.isArray proto[method].validate
              args_format = 'ARRAY'
              decs        = proto[method].validate

              proto[method].validators = for val, i in decs
                "#{i}": Madul.PARSE_SPEC(val).ref
            else if typeof proto[method].validate == 'object'
              args_format = 'OBJECT'
              decs        = [ ]

              for own key, val of proto[method].validate
                if Array.isArray val
                  for v in val
                    decs.push v
                else if typeof val == 'object'
                  for own k, v of val
                    if Array.isArray v
                      for V in v
                        decs.push V
                    else
                      decs.push v
                else
                  decs.push val

              proto[method].validators = [ ]

              for own key, val of proto[method].validate
                if Array.isArray val
                  proto[method].validators.push "#{key}": for v in val
                    Madul.PARSE_SPEC(v).ref
                else if typeof val == 'object'
                  for own k, v of val
                    if Array.isArray v
                      proto[method].validators.push "#{key}.#{k}": for V in v
                        Madul.PARSE_SPEC(V).ref
                    else
                      proto[method].validators.push "#{key}.#{k}": Madul.PARSE_SPEC(v).ref
                else
                  proto[method].validators.push "#{key}": Madul.PARSE_SPEC(val).ref
            else if typeof proto[method].validate == 'string'
              args_format = 'ARRAY'
              decs        = [ proto[method].validate ]

              proto[method].validators = [ { 0: Madul.PARSE_SPEC(proto[method].validate).ref }]
            else if typeof proto[method].validate != 'undefined'
              proto.warn.call proto, 'meta.invalid', validate: 'must be a String, Object, or Array'
              return
            else
              args_format = 'ARRAY'
              decs        = [ ]

            if Array.isArray proto[method].before
              decs = decs.concat proto[method].before
            else if typeof proto[method].before == 'string'
              decs = decs.concat [ proto[method].before ]

            if Array.isArray proto[method].after
              decs = decs.concat proto[method].after
            else if typeof proto[method].after == 'string'
              decs = decs.concat [ Madul.PARSE_SPEC proto[method].after ]

            deduped = decs.filter (e, i, a) => a.indexOf(e) == i

            proto.fire.call proto, 'decorators.hydrate', deduped

            handle_err = (fn, args, next, reject) =>
              (err) =>
                proto.warn.call proto, "#{fn}.validator.failed", args
                reject err

                stop = new Error()
                stop.break = true

                next stop

            @_do_hydrate proto, deduped, =>
              proto["_#{method}"] = proto[method]

              proto[method] = =>
                def = q.defer()
                me  = proto["_#{method}"]

                if args_format == 'ARRAY'
                  args = Array.prototype.slice.call arguments
                else if args_format == 'OBJECT'
                  args = arguments[0]
                else
                  proto.warn.call proto, 'cannot-continue', 'No arg format specified'

                  return def.reject 'No arg format specified'

                before = @_process_decorators me, 'before'
                after  = @_process_decorators me, 'after'

                q.Promise (resolve, reject) =>
                  async.eachSeries me.validators, (v, next) =>
                    arg       = Object.keys(v)[0]
                    validator = v[arg]
                    input     = arg
                    prop      = args

                    for a in arg.split '.'
                      prop  = prop[a]
                      input = a

                    if Array.isArray validator
                      async.eachSeries validator, (v, nxt) =>
                        proto.fire.call proto, 'validator.execute', "#{v}": prop

                        proto[v].EXECUTE.call proto[v], input, prop
                          .then nxt
                          .catch handle_err v, { "#{input}": prop }, nxt, reject
                      , next
                    else
                      proto.fire.call proto, 'validator.execute', "#{validator}": prop

                      proto[validator].EXECUTE.call proto[validator], input, prop
                        .then next
                        .catch handle_err v, { "#{input}": prop }, next, reject
                  , resolve
                .then =>
                  q.Promise (resolve, reject) =>
                    async.eachSeries before, (b, next) =>
                      proto.fire.call proto, 'before_filter.execute', "#{b}": args

                      proto[b].before.call proto[b], args
                        .then next
                        .catch handle_err b, args, next, reject
                    , resolve
                .then =>
                  input = proto._prep_invocation proto, method, args, def

                  try
                    me.behavior.apply me, input
                  catch e
                    failed = if input[0].fail? then input[0].fail else input[input.length - 2]

                    me.warn.call me, 'runtime-exception', stack: e.stack, message: e.message

                    failed e.message
                .then (result) =>
                  q.Promise (resolve, reject) =>
                    if after.length > 0
                      async.eachSeries after, (a, next) =>
                        proto.fire.call proto, 'after_filter.execute', "#{a}": args

                        proto[a].after.call proto[a], result
                          .then next
                          .catch handle_err b, args, next, reject
                      , => resolve result
                    else
                      process.nextTick => resolve result
                .then  (out) => def.resolve out
                .catch (err) => def.reject  err

                def.promise

              INITIALIZERS proto.constructor.name, method if method[0] == '$'

              wrapped()
          else
            proto.warn.call proto, 'cannot-wrap', method

          WRAPPED[proto.constructor.name].push method

      _wrap_methods: (proto, callback) =>
        name    = proto.constructor.name
        to_wrap = [ ]

        while proto? && Object.keys(proto).length > 0 && WRAPPED[name]? == false
          to_wrap.push { proto, name, methods: [ ] }

          for own prop, body of proto
            if prop[0]                  != '_'         &&
               typeof proto["_#{prop}"] == 'undefined' &&
               WRAP(name, prop)         == true

              continue if proto.deps? && proto.deps.indexOf(prop.replace(/_/g, '-')) > -1

              if typeof body == 'function' || typeof body?.behavior == 'function'
                to_wrap[to_wrap.length - 1].methods.push prop

          proto = proto.__proto__
          name  = proto.constructor.name

        async.each to_wrap.reverse(), (wrap, completed) =>
          async.each wrap.methods, (method, next) =>
            wrap.proto._do_wrap wrap.proto, method, next
          , => @_call_initializers_for wrap.proto, wrap.name, completed
        , callback

      _make_available: (name, mod) =>
        Madul.FIRE "$.#{name}.available"

        available[strip name] = mod

      _hydrate_deps: (proto, done) =>
        protos = [ proto ]

        if proto.__proto__?
          while proto.__proto__? && Object.keys(proto.__proto__).length > 0
            protos.push proto = proto.__proto__

        async.each protos.reverse(), (p, next) =>
          name = p.constructor.name

          if p.hasOwnProperty 'deps'
            if HYDRATED[name] == undefined
              HYDRATED[name]            = false
              HYRDATION_LISTENERS[name] = [ next ]

              p._do_hydrate p, p.deps, =>
                HYDRATED[name] = true

                for listener in HYRDATION_LISTENERS[name]
                  listener()
            else if HYDRATED[name] == false
              HYRDATION_LISTENERS[name].push next
            else if HYDRATED[name] == true
              next()
          else
            next()
        , done

      _init_if_madul: (path, callback) =>
        mod      = require path
        proto    = mod
        is_madul = false

        while proto
          is_madul = true if proto.name == 'Madul'
          if is_madul                            == false      &&
             proto.constructor.name              != 'Object'   &&
             typeof proto                        == 'function' &&
             typeof proto.__super__?.constructor == 'function'
            proto = proto.__super__.constructor
          else
            proto = undefined

        if is_madul
          new mod()
            .initialize()
            .then callback
        else
          callback mod

      _load: (me, name, path, loaded) =>
        me._init_if_madul path, (mod) =>
          me._make_available name, mod
          loaded mod

      _check: (me, path, dep, ref, finished) =>
        error = true

        fs.readdir path, (err, files) =>
          if err?
            me.warn 'file-not-found', err

            finished 'NOT_FOUND'
          else
            async.each files, (f, next) =>
              depth = "#{path}/#{f}"

              fs.stat depth, (e, s) =>
                if s.isDirectory() && f[0] != '.'
                  me._check me, depth, dep, ref, next
                else if s.isFile() && f.substring(0, f.length - 3) == dep
                  me._load me, ref, depth, (mod) =>
                    me._do_add me, ref, next
                    error = undefined
                else
                  next()
            , => finished error

      _do_add: (me, ref, next) =>
        Madul.FIRE "$.#{me.constructor.name}.dependency.register", ref

        me[underscore ref] = available[strip ref]

        next()

      _add: (me, ref, path, next) =>
        me._init_if_madul path, (mod) =>
          me._make_available ref, mod
          me._do_add me, ref, next

      _do_hydrate: (proto, deps, hydration_complete) =>
        initers = [ ]

        async.each deps, (d, next) =>
          spec = Madul.PARSE_SPEC d

          if spec.prerequisites?
            insert_at = for p in spec.prerequisites
              index_for = (sibling) => sibling.alias == p.alias

              initers.find index_for

            insert_at.sort()

            insert_at = if insert_at.length == 0 then 0 else insert_at[insert_at.length - 1]

            initers.splice insert_at, 0, alias: spec.alias, execute: spec.initializer
          else if spec.initializer?
            initers.push alias: spec.alias, execute: spec.initializer

          if typeof proto[underscore spec.ref] == 'undefined'
            if available[strip spec.ref]? == true
              proto._do_add proto, spec.ref, next
            else
              if spec.parent?
                proto.fire.call proto, 'search_root.load', { name: spec.name, alias: spec.alias, parent: spec.parent }

                @_do_hydrate proto, [ spec.parent ], =>
                  if SEARCH_ROOTS[spec.parent] == undefined
                    SEARCH_ROOTS[spec.parent] = @_find_code_root spec.parent

                  proto._check proto, SEARCH_ROOTS[spec.parent], spec.name, spec.ref, next
              else if spec.node_local
                key = @_find_require_key proto.constructor.name

                async.eachSeries require.cache[key].paths, (path, nxt) =>
                  @_load_from_package_json proto, path, spec.name, spec.ref, =>
                    stop = new Error()
                    stop.break = true

                    next()
                    nxt stop
                  , nxt
              else if spec.project_local
                proto._check proto, LOCALS[proto.constructor.name], spec.name, spec.ref, next
              else
                try
                  proto._add proto, spec.ref, spec.name, next
                catch e
                  path = "#{process.cwd()}/node_modules"

                  @_load_from_package_json proto, path, spec.name, spec.ref, next, =>
                    pth = LOCALS[proto.constructor.name]

                    try
                      proto._check proto, pth, spec.name, spec.ref, (err) =>
                        if err?
                          proto._check proto, "#{process.cwd()}/dist", spec.name, spec.ref, next
                        else
                          next()

          else
            next()
        , =>
          async.eachSeries initers, (initer, next) =>
            proto[initer.execute].apply(@).then next
          , hydration_complete

      _load_from_package_json: (proto, path, name, ref, success, fail) =>
        pkg = "#{path}/#{name}/package.json"

        fs.stat pkg, (err, stat) =>
          if stat?.isFile()
            fs.readFile pkg, 'utf8', (err, data) =>
              json = JSON.parse data
              main = json.main || json._main || 'index.js'
              path = "#{path}/#{name}/#{main}"

              proto._add proto, ref, path, success
          else
            fail()

      _finish_up: (proto, args) =>
        name = proto.constructor.name

        if listeners[name]?
          async.each listeners[name], (listener, next) =>
            listener @
            next()
          , => @_register_initialized name
        else
          @_register_initialized name

      _register_initialized: (name) =>
        @_make_available name, @

        initialized[name] = true

        Madul.FIRE "$.#{name}.initialized"

      _call_initializers_for: (proto, name, callback) =>
        if _INITIALIZERS[name]?
          async.each _INITIALIZERS[name], (initializer, next) =>
            if initializer.called == false
              proto[initializer.fn].apply proto
                .then =>
                  initializer.called = true
                  next()
            else
              next()
          , callback
        else
          callback()

      _do_find: (thing, cb) =>
        file  = thing.toLowerCase()
        found = for own key, val of require.cache
          if key.replace(/_/g, '').toLowerCase().includes file
            cb key

        found
          .filter (f) => f?
          .sort((a, b) => a.length - b.length)[0]

      _find_require_key: (resource) =>
        @_do_find resource, (key) => key

      _find_code_root: (resource) =>
        @_do_find resource, (key) => key.substring(0, key.lastIndexOf '/').split '/'
          .join '/'

      initialize: =>
        deferred = q.defer()
        proto    = @.__proto__
        name     = proto.constructor.name

        Madul.FIRE "$.#{name}.request_instance"

        if initialized[name] == undefined
          Madul.FIRE "$.#{name}.initialize"

          initialized[name] = false
          listeners[name]   = [ deferred.resolve ]

          if LOCALS[name] == undefined
            LOCALS[name] = @_find_code_root name

          @_hydrate_deps proto, => @_wrap_methods proto, => @_finish_up proto
        else if initialized[name] == false
          listeners[name].push deferred.resolve
        else if initialized[name] == true
          process.nextTick => deferred.resolve available[strip name]

        deferred.promise

    module.exports = Madul

    q     = require 'q'
    fs    = require 'fs'
    async = require 'async'
    uuid  = require 'uuid-1345'

    microtime = require 'microtime'

    EventEmitter2 = require('eventemitter2').EventEmitter2
    emitter       = new EventEmitter2 wildcard: true

    available   = { }
    initialized = { }
    listeners   = { }

    underscore = (value) -> value.replace /\W+/g, '_'
    strip      = (name)  -> name.toLowerCase().replace /\W+/g, ''

    SEARCH_ROOTS  = { }
    WRAPPED       = { }
    _INITIALIZERS = { }
    HYDRATED      = { }
    LOCALS        = { }

    SKIP_WRAP = [
      'constructor'
      'initialize'
      'details'
      'fire'
      'warn'
      'listen'
      'set'
      'runtime_exception'
    ]

    HYRDATION_LISTENERS = { }

    INITIALIZERS = (cls, method) ->
      _INITIALIZERS[cls] = [ ] unless _INITIALIZERS[cls]?

      _INITIALIZERS[cls].push fn: method, called: false

    WRAP = (cls, method) ->
      WRAPPED[cls] = [ ] unless WRAPPED[cls]?

      WRAPPED[cls].indexOf(method) < 0

    determine_local = (token) ->
      project_local = false
      node_local    = false
      name          = token.substring 1, token.length

      if token[0] == '.'
        project_local = true
      else if token[0] == '~'
        node_local = true
      else
        name = token

      [project_local, node_local, name]

    parse_name_and_local = (tokens) ->
      [project_local, node_local, name] = determine_local tokens[0].trim()

      if tokens.length == 2
        alias = tokens[1].trim()
      else if tokens.length == 1
        alias = name
      else
        return Madul.FIRE '!.Madul.dependency_spec.alias.invalid', tokens[1]

      [project_local, node_local, name, alias]

    parse_alias_and_parent = (token) ->
      t = token.split '->'

      if t[0].indexOf('#') > -1
        r      = t[0].split '#'
        parent = r[0].trim()
        t[0]   = r[1]

      [parent, t]

    extract_properties = (token) ->
      [parent, t] = parse_alias_and_parent token
      [project_local, node_local, name, alias] = parse_name_and_local t

      [project_local, node_local, name, alias, parent]

    class Madul

      details: =>
        name  = @.__proto__.constructor.name
        props = for own key, val of @.__proto__
          if key != 'constructor' &&
             key != 'details'     &&
             key != 'initialize'  && ((
               key[0]            == '_' &&
               @.__proto__[key]? == false
             ) || key[0] != '_')

            if typeof val == 'function'
              value = val.constructor.name
            else if Array.isArray val
              value = for v in val
                if typeof v == 'function'
                  v.constructor.name
                else if typeof v == 'object'
                  v.constructor.name
                else
                  v
              value = value.join ','
            else if typeof val == 'object'
              value = val.constructor.name
            else
              value = val

            "#{key} => #{value}"

        "\n  #{name}\n    #{props.filter((e) -> e?).join '\n    '}"

      @LISTEN: (event, callback) => emitter.on event, callback

      @FIRE: (event, args) => emitter.emit event, args

      @SEARCH_ROOT: (mod, path) =>
        Madul.FIRE '$.Madul.search_root.register', "#{mod}": path

        SEARCH_ROOTS[mod] = path.substring 0, path.lastIndexOf '/'

      @PARSE_SPEC: (value) =>
        tokens = value.split '='

        [project_local, node_local, name, alias, parent] = extract_properties tokens[0]

        if tokens.length == 2
          initializer = tokens[1].trim()
          toks        = initializer.split ':'
          initializer = toks[0].trim()

          if toks.length == 2
            prerequisites = toks[1].split(',').map (p) => p.trim()
          else if toks.length > 2
            return Madul.FIRE '!.Madul.dependency_spec.initializer.invalid', tokens[1]

        else if tokens.length > 2
          return Madul.FIRE '!.Madul.dependency_spec.invalid', d

        { ref: alias || name, alias, name, parent, initializer, prerequisites, project_local, node_local }

      @BUILD_SPEC: ({ search_root, name, alias, initializer, prerequisites }) =>
        unless name?
          return Madul.FIRE '!.Madul.dependency_spec.name-required', arguments[0]

        if prerequisites? && initializer? == false
          return Madul.FIRE '!.Madul.dependency_spec.prerequisites-require-initializer', arguments[0]

        "#{if search_root?   then "#{search_root}#"            else ''}\
         #{name}\
         #{if alias?         then " -> #{alias}"               else ''}\
         #{if initializer?   then " = #{initializer}"          else ''}\
         #{if prerequisites? then ":#{prerequisites.join ','}" else ''}\
        "

      listen: (event, callback) ->
        Madul.LISTEN "*.#{@constructor.name}.#{event}", callback

      fire: (event, args) ->
        Madul.FIRE "@.#{@constructor.name}.#{event}", args

      warn: (event, details, finisher) =>
        Madul.FIRE "!.#{@constructor.name}.#{event}", details

        finisher() if typeof finisher == 'function'

      runtime_exception: (exit = false) =>
        (err) =>
          Madul.FIRE "!.#{@constructor.name}.runtime-exception",
            stack:     err.stack
            message:   err.message
            timestamp: microtime.now()

          process.exit(1) if exit == 'DIE_ON_EXCEPTION'

      set: (prop, val) =>
        proto = @.__proto__
        mod   = available[strip proto.constructor.name.toLowerCase()]

        if mod?
          mod.__proto__[prop] = val

      _report: (method, state, id, args, breaker) =>
        invoke = undefined
        params =
          uuid:      id
          args:      args
          timestamp: microtime.now()

        if state == 'reject'
          if typeof breaker == 'function'
            stop = new Error()
            stop.break = true

            breaker stop

          @warn "#{method}.#{state}", params
        else
          @fire "#{method}.#{state}", params

      _respond: (proto, method, id, deferred) =>
        (state, data, breaker) =>
          process.nextTick =>
            deferred[state] data

            proto._report method, state, id, data, breaker

      _prep_invocation: (proto, method, input, def) =>
        id  = uuid.v4fast()
        res = proto._respond proto, method, id, def
        e   = (brk) =>
          error = (err) => res 'reject', err, brk

          if typeof brk == 'function'
            error
          else
            error brk

        if Array.isArray input
          input.push (out) => res 'resolve', out
          input.push e
          input.push (out) => res 'notify',  out
        else if typeof input == 'object'
          input.done   = (out) => res 'resolve', out
          input.fail   = e
          input.update = (out) => res 'notify',  out

          input = [ input ]
        else
          msg = "input to #{proto.constructor.name}.#{method} must be an Array of Object"

          proto.warn.call proto, 'input.invalid', msg

          throw new Error msg

        proto._report method, 'invoke', id, input

        input

      _process_decorators: (proto, type) =>
        if Array.isArray proto[type]
          proto[type].map (b) => Madul.PARSE_SPEC(b).ref
        else if typeof proto[type] == 'string'
          [ Madul.PARSE_SPEC(proto[type]).ref ]
        else
          [ ]

      _do_wrap: (proto, method, wrapped) =>
        if SKIP_WRAP.includes method
          wrapped()
        else
          Madul.FIRE "$.#{proto.constructor.name}.#{method}.wrap"

          if typeof proto[method] == 'function'
            proto["_#{method}"] = proto[method]

            proto[method] = =>
              def   = q.defer()
              args  = Array.prototype.slice.call arguments
              input = proto._prep_invocation proto, method, args, def

              try
                proto["_#{method}"].apply proto, input
              catch e
                failed = if input[0].fail? then input[0].fail else input[input.length - 2]

                proto.warn.call proto, 'runtime-exception', stack: e.stack, message: e.message

                failed e.message

              def.promise

            INITIALIZERS proto.constructor.name, method if method[0] == '$'

            wrapped()
          else if typeof proto[method] == 'object'

            if Array.isArray proto[method].validate
              args_format = 'ARRAY'
              decs        = proto[method].validate

              proto[method].validators = for val, i in decs
                "#{i}": Madul.PARSE_SPEC(val).ref
            else if typeof proto[method].validate == 'object'
              args_format = 'OBJECT'
              decs        = [ ]

              for own key, val of proto[method].validate
                if Array.isArray val
                  for v in val
                    decs.push v
                else if typeof val == 'object'
                  for own k, v of val
                    if Array.isArray v
                      for V in v
                        decs.push V
                    else
                      decs.push v
                else
                  decs.push val

              proto[method].validators = [ ]

              for own key, val of proto[method].validate
                if Array.isArray val
                  proto[method].validators.push "#{key}": for v in val
                    Madul.PARSE_SPEC(v).ref
                else if typeof val == 'object'
                  for own k, v of val
                    if Array.isArray v
                      proto[method].validators.push "#{key}.#{k}": for V in v
                        Madul.PARSE_SPEC(V).ref
                    else
                      proto[method].validators.push "#{key}.#{k}": Madul.PARSE_SPEC(v).ref
                else
                  proto[method].validators.push "#{key}": Madul.PARSE_SPEC(val).ref
            else if typeof proto[method].validate == 'string'
              args_format = 'ARRAY'
              decs        = [ proto[method].validate ]

              proto[method].validators = [ { 0: Madul.PARSE_SPEC(proto[method].validate).ref }]
            else if typeof proto[method].validate != 'undefined'
              proto.warn.call proto, 'meta.invalid', validate: 'must be a String, Object, or Array'
              return
            else
              args_format = 'ARRAY'
              decs        = [ ]

            if Array.isArray proto[method].before
              decs = decs.concat proto[method].before
            else if typeof proto[method].before == 'string'
              decs = decs.concat [ proto[method].before ]

            if Array.isArray proto[method].after
              decs = decs.concat proto[method].after
            else if typeof proto[method].after == 'string'
              decs = decs.concat [ Madul.PARSE_SPEC proto[method].after ]

            deduped = decs.filter (e, i, a) => a.indexOf(e) == i

            Madul.FIRE '$.Madul.decorators.hydrate', deduped

            handle_err = (fn, args, next, reject) =>
              (err) =>
                proto.warn.call proto, "#{fn}.validator.failed", args
                reject err

                stop = new Error()
                stop.break = true

                next stop

            @_do_hydrate proto, deduped, =>
              proto["_#{method}"] = proto[method]

              proto[method] = =>
                def = q.defer()
                me  = proto["_#{method}"]

                if args_format == 'ARRAY'
                  args = Array.prototype.slice.call arguments
                else if args_format == 'OBJECT'
                  args = arguments[0]
                else
                  proto.warn.call proto, 'cannot-continue', 'No arg format specified'

                  return def.reject 'No arg format specified'

                before = @_process_decorators me, 'before'
                after  = @_process_decorators me, 'after'

                q.Promise (resolve, reject) =>
                  async.eachSeries me.validators, (v, next) =>
                    arg       = Object.keys(v)[0]
                    validator = v[arg]
                    input     = arg
                    prop      = args

                    for a in arg.split '.'
                      prop  = prop[a]
                      input = a

                    if Array.isArray validator
                      async.eachSeries validator, (v, nxt) =>
                        proto.fire.call proto, 'validator.execute', "#{v}": prop

                        proto[v].EXECUTE.call proto[v], input, prop
                          .then nxt
                          .catch handle_err v, { "#{input}": prop }, nxt, reject
                      , next
                    else
                      proto.fire.call proto, 'validator.execute', "#{validator}": prop

                      proto[validator].EXECUTE.call proto[validator], input, prop
                        .then next
                        .catch handle_err v, { "#{input}": prop }, next, reject
                  , resolve
                .then =>
                  q.Promise (resolve, reject) =>
                    async.eachSeries before, (b, next) =>
                      proto.fire.call proto, 'before_filter.execute', "#{b}": args

                      proto[b].before.call proto[b], args
                        .then next
                        .catch handle_err b, args, next, reject
                    , resolve
                .then =>
                  input = proto._prep_invocation proto, method, args, def

                  try
                    me.behavior.apply me, input
                  catch e
                    failed = if input[0].fail? then input[0].fail else input[input.length - 2]

                    me.warn.call me, 'runtime-exception', stack: e.stack, message: e.message

                    failed e.message
                .then (result) =>
                  q.Promise (resolve, reject) =>
                    if after.length > 0
                      async.eachSeries after, (a, next) =>
                        proto.fire.call proto, 'after_filter.execute', "#{a}": args

                        proto[a].after.call proto[a], result
                          .then next
                          .catch handle_err b, args, next, reject
                      , => resolve result
                    else
                      process.nextTick => resolve result
                .then  (out) => def.resolve out
                .catch (err) => def.reject  err

                def.promise

              INITIALIZERS proto.constructor.name, method if method[0] == '$'

              wrapped()
          else
            proto.warn.call proto, 'cannot-wrap', method

          WRAPPED[proto.constructor.name].push method

      _wrap_methods: (proto, callback) =>
        name    = proto.constructor.name
        to_wrap = [ ]

        while proto? && Object.keys(proto).length > 0 && WRAPPED[name]? == false
          to_wrap.push { proto, name, methods: [ ] }

          for own prop, body of proto
            if prop[0]                  != '_'         &&
               typeof proto["_#{prop}"] == 'undefined' &&
               WRAP(name, prop)         == true

              continue if proto.deps? && proto.deps.indexOf(prop.replace(/_/g, '-')) > -1

              if typeof body == 'function' || typeof body?.behavior == 'function'
                to_wrap[to_wrap.length - 1].methods.push prop

          proto = proto.__proto__
          name  = proto.constructor.name

        async.each to_wrap.reverse(), (wrap, completed) =>
          async.each wrap.methods, (method, next) =>
            wrap.proto._do_wrap wrap.proto, method, next
          , => @_call_initializers_for wrap.proto, wrap.name, completed
        , callback

      _make_available: (name, mod) =>
        Madul.FIRE "$.#{name}.available"

        available[strip name] = mod

      _hydrate_deps: (proto, done) =>
        protos = [ proto ]

        if proto.__proto__?
          while proto.__proto__? && Object.keys(proto.__proto__).length > 0
            protos.push proto = proto.__proto__

        async.each protos.reverse(), (p, next) =>
          name = p.constructor.name

          if p.hasOwnProperty 'deps'
            if HYDRATED[name] == undefined
              HYDRATED[name]            = false
              HYRDATION_LISTENERS[name] = [ next ]

              p._do_hydrate p, p.deps, =>
                HYDRATED[name] = true

                for listener in HYRDATION_LISTENERS[name]
                  listener()
            else if HYDRATED[name] == false
              HYRDATION_LISTENERS[name].push next
            else if HYDRATED[name] == true
              next()
          else
            next()
        , done

      _init_if_madul: (path, callback) =>
        mod      = require path
        proto    = mod
        is_madul = false

        while proto
          is_madul = true if proto.name == 'Madul'
          if is_madul                            == false      &&
             proto.constructor.name              != 'Object'   &&
             typeof proto                        == 'function' &&
             typeof proto.__super__?.constructor == 'function'
            proto = proto.__super__.constructor
          else
            proto = undefined

        if is_madul
          new mod()
            .initialize()
            .then callback
        else
          callback mod

      _load: (me, name, path, loaded) =>
        me._init_if_madul path, (mod) =>
          me._make_available name, mod
          loaded mod

      _check: (me, path, dep, ref, finished) =>
        error = true

        fs.readdir path, (err, files) =>
          if err?
            me.warn 'file-not-found', err

            finished 'NOT_FOUND'
          else
            async.each files, (f, next) =>
              depth = "#{path}/#{f}"

              fs.stat depth, (e, s) =>
                if s.isDirectory() && f[0] != '.'
                  me._check me, depth, dep, ref, next
                else if s.isFile() && f.substring(0, f.length - 3) == dep
                  me._load me, ref, depth, (mod) =>
                    me._do_add me, ref, next
                    error = undefined
                else
                  next()
            , => finished error

      _do_add: (me, ref, next) =>
        Madul.FIRE "$.#{me.constructor.name}.dependency.register", ref

        me[underscore ref] = available[strip ref]

        next()

      _add: (me, ref, path, next) =>
        me._init_if_madul path, (mod) =>
          me._make_available ref, mod
          me._do_add me, ref, next

      _do_hydrate: (proto, deps, hydration_complete) =>
        initers = [ ]

        async.each deps, (d, next) =>
          spec = Madul.PARSE_SPEC d

          if spec.prerequisites?
            insert_at = for p in spec.prerequisites
              index_for = (sibling) => sibling.alias == p.alias

              initers.find index_for

            insert_at.sort()

            insert_at = if insert_at.length == 0 then 0 else insert_at[insert_at.length - 1]

            initers.splice insert_at, 0, alias: spec.alias, execute: spec.initializer
          else if spec.initializer?
            initers.push alias: spec.alias, execute: spec.initializer

          if typeof proto[underscore spec.ref] == 'undefined'
            if available[strip spec.ref]? == true
              proto._do_add proto, spec.ref, next
            else
              if spec.parent?
                Madul.FIRE '$.Madul.search_root.load', { name: spec.name, alias: spec.alias, parent: spec.parent }

                @_do_hydrate proto, [ spec.parent ], =>
                  if SEARCH_ROOTS[spec.parent] == undefined
                    SEARCH_ROOTS[spec.parent] = @_find_code_root spec.parent

                  proto._check proto, SEARCH_ROOTS[spec.parent], spec.name, spec.ref, next
              else if spec.node_local
                key = @_find_require_key proto.constructor.name

                async.eachSeries require.cache[key].paths, (path, nxt) =>
                  @_load_from_package_json proto, path, spec.name, spec.ref, =>
                    stop = new Error()
                    stop.break = true

                    next()
                    nxt stop
                  , nxt
              else if spec.project_local
                proto._check proto, LOCALS[proto.constructor.name], spec.name, spec.ref, next
              else
                try
                  proto._add proto, spec.ref, spec.name, next
                catch e
                  path = "#{process.cwd()}/node_modules"

                  @_load_from_package_json proto, path, spec.name, spec.ref, next, =>
                    pth = LOCALS[proto.constructor.name]

                    try
                      proto._check proto, pth, spec.name, spec.ref, (err) =>
                        if err?
                          proto._check proto, "#{process.cwd()}/dist", spec.name, spec.ref, next
                        else
                          next()

          else
            next()
        , =>
          async.eachSeries initers, (initer, next) =>
            proto[initer.execute].apply(@).then next
          , hydration_complete

      _load_from_package_json: (proto, path, name, ref, success, fail) =>
        pkg = "#{path}/#{name}/package.json"

        fs.stat pkg, (err, stat) =>
          if stat?.isFile()
            fs.readFile pkg, 'utf8', (err, data) =>
              json = JSON.parse data
              main = json.main || json._main || 'index.js'
              path = "#{path}/#{name}/#{main}"

              proto._add proto, ref, path, success
          else
            fail()

      _finish_up: (proto, args) =>
        name = proto.constructor.name

        if listeners[name]?
          async.each listeners[name], (listener, next) =>
            listener @
            next()
          , => @_register_initialized name
        else
          @_register_initialized name

      _register_initialized: (name) =>
        @_make_available name, @

        initialized[name] = true

        Madul.FIRE "$.#{name}.initialized"

      _call_initializers_for: (proto, name, callback) =>
        if _INITIALIZERS[name]?
          async.each _INITIALIZERS[name], (initializer, next) =>
            if initializer.called == false
              proto[initializer.fn].apply proto
                .then =>
                  initializer.called = true
                  next()
            else
              next()
          , callback
        else
          callback()

      _do_find: (thing, cb) =>
        file  = thing.toLowerCase()
        found = for own key, val of require.cache
          if key.replace(/_/g, '').toLowerCase().includes file
            cb key

        found
          .filter (f) => f?
          .sort((a, b) => a.length - b.length)[0]

      _find_require_key: (resource) =>
        @_do_find resource, (key) => key

      _find_code_root: (resource) =>
        @_do_find resource, (key) => key.substring(0, key.lastIndexOf '/').split '/'
          .join '/'

      initialize: =>
        deferred = q.defer()
        proto    = @.__proto__
        name     = proto.constructor.name

        Madul.FIRE "$.#{name}.request_instance"

        if initialized[name] == undefined
          Madul.FIRE "$.#{name}.initialize"

          initialized[name] = false
          listeners[name]   = [ deferred.resolve ]

          if LOCALS[name] == undefined
            LOCALS[name] = @_find_code_root name

          @_hydrate_deps proto, => @_wrap_methods proto, => @_finish_up proto
        else if initialized[name] == false
          listeners[name].push deferred.resolve
        else if initialized[name] == true
          process.nextTick => deferred.resolve available[strip name]

        deferred.promise

    module.exports = Madul


Attach exit signals ASAP

Attach exit signals ASAP

# Server application entry point

*Load the configuration, invoke the bootstrap hooks, and listen for signals
and process exit.*

The core bootstrap phase injects environment into a forked copy of the
application including require paths to allow core and custom packages to be
included without qualification.

    {fork} = require "./server/bootstrap"
    unless fork()

      Promise = require 'bluebird'

      debug = require('debug') 'shrub:server'
      debugSilly = require('debug') 'shrub-silly:server'

      errors = require 'errors'
      middleware = require 'middleware'
      pkgman = require 'pkgman'

Set up exit hooks.

#### Invoke hook `shrubCoreProcessExit`.

      process.on 'exit', -> pkgman.invoke 'shrubCoreProcessExit'

      process.on 'SIGINT', -> process.exit()
      process.on 'SIGTERM', -> process.exit()
      process.on 'unhandledException', -> process.exit()

Load the configuration.

      debug 'Loading config...'

      config = require 'config'
      config.load()
      config.loadPackageSettings()

      debug 'Config loaded.'

#### Invoke hook `shrubCorePreBootstrap`.

      debugSilly 'Pre bootstrap phase...'
      pkgman.invoke 'shrubCorePreBootstrap'
      debugSilly 'Pre bootstrap phase completed.'

#### Invoke hook `shrubCoreBootstrapMiddleware`.

      debugSilly 'Loading bootstrap middleware...'

      bootstrapMiddleware = middleware.fromConfig(
        'shrub-core:bootstrapMiddleware'
      )

      middleware.fromHook(
        'shrubCoreBootstrapMiddleware'
        config.get 'packageSettings:shrub-core:shrubCoreBootstrapMiddleware'
      )

      debugSilly 'Bootstrap middleware loaded.'

Dispatch the bootstrap middleware stack and log if everything is okay.

      bootstrapMiddleware.dispatch (error) ->
        return debug 'Bootstrap complete.' unless error?

Log and throw any error. This will be caught by the unhandledException
listener below.

        console.error errors.stack error
        throw error

# Server application entry point

*Load the configuration, invoke the bootstrap hooks, and listen for signals
and process exit.*

The core bootstrap phase injects environment into a forked copy of the
application including require paths to allow core and custom packages to be
included without qualification.

    {fork} = require "./server/bootstrap"
    unless fork()

      Promise = require 'bluebird'

      debug = require('debug') 'shrub:server'
      debugSilly = require('debug') 'shrub-silly:server'

      errors = require 'errors'
      middleware = require 'middleware'
      pkgman = require 'pkgman'

Load the configuration.

      debug 'Loading config...'

      config = require 'config'
      config.load()
      config.loadPackageSettings()

      debug 'Config loaded.'

#### Invoke hook `shrubCorePreBootstrap`.

      debugSilly 'Pre bootstrap phase...'
      pkgman.invoke 'shrubCorePreBootstrap'
      debugSilly 'Pre bootstrap phase completed.'

#### Invoke hook `shrubCoreBootstrapMiddleware`.

      debugSilly 'Loading bootstrap middleware...'

      bootstrapMiddleware = middleware.fromConfig(
        'shrub-core:bootstrapMiddleware'
      )

      middleware.fromHook(
        'shrubCoreBootstrapMiddleware'
        config.get 'packageSettings:shrub-core:shrubCoreBootstrapMiddleware'
      )

      debugSilly 'Bootstrap middleware loaded.'

Dispatch the bootstrap middleware stack and log if everything is okay.

      bootstrapMiddleware.dispatch (error) ->
        return debug 'Bootstrap complete.' unless error?

Log and throw any error. This will be caught by the unhandledException
listener below.

        console.error errors.stack error
        throw error

#### Invoke hook `shrubCoreProcessExit`.

      process.on 'exit', -> pkgman.invoke 'shrubCoreProcessExit'

      process.on 'SIGINT', -> process.exit()
      process.on 'SIGTERM', -> process.exit()
      process.on 'unhandledException', -> process.exit()


only show oneof selection when there are options

Copyright 2014 - 2015
Author: Varuna Jayasiri http://blog.varunajayasiri.com

#OneOf

    Mod.require 'Models.Properties',
     'Models.Property.Base'
     'Models.Models'
     'Weya.Base'
     'Weya'
     (PROPERTIES, Base, MODELS, WeyaBase, Weya) ->

Class

      class OneOf extends Base
       @extend()

       propertyType: 'oneof'

       @default 'oneof', ['Null']
       @default 'default', ->
        return null if @schema.oneof.length is 0
        model = new (MODELS.get @schema.oneof[0])
        r = model.parse @schema.defaultValues.call this
        return r.value

       @default 'defaultValues', -> {}

       @default 'isDefault', (value) ->
        return true if @schema.oneof.length is 0
        model = new (MODELS.get @schema.oneof[0])
        model.parse value
        model.isDefault()

       @isValidSchema: (schema) ->
        return false if not super schema
        return false if not schema.oneof?
        return true

       parse: (data) ->
        r = super data
        return r unless r is true

        res =
         score: 0
         errors: ['invalid']
         value: @schema.default.call this
        for type in @schema.oneof
         m = new (MODELS.get type)
         r = m.parse data
         if res.score < r.score
          res = r

        return res

       toJSON: (value) -> value.toJSON()
       toJSONFull: (value) -> value.toJSONFull()

       edit: (elem, value, changed) ->
        new Edit this, elem, value, changed

Edit

      class Edit extends WeyaBase
       @extend()

       @initialize (property, elem, value, changed) ->
        @property = property
        @elems =
         parent: elem
        @model = value
        @onChanged = changed
        @render()

       render: ->
        Weya elem: @elems.parent, context: this, ->
         @$.elems.controls = @div ".oneof-controls", ->
          @$.elems.type = @select on: {change: @$.on.typeChange}, ->
           for type in @$.property.schema.oneof
            @option value: type, type

         @$.elems.model = @div ".model", null

        if @property.schema.oneof.length <= 1
         @elems.controls.style.display = 'none'

        @renderModel()

       @listen 'typeChange', (e) ->
        type = @elems.type.value
        return if type is @model.type
        json = @model.toJSON()
        @model = new (MODELS.get type)
        r = @model.parse json
        @onChanged @model, true
        @renderModel()

       renderModel: ->
        @elems.model.innerHTML = ''
        @elems.type.value = @model.type
        @model.edit @elems.model, @modelChanged.bind this

       modelChanged: (value) ->
        @onChanged value, false

      PROPERTIES.register 'oneof', OneOf

Copyright 2014 - 2015
Author: Varuna Jayasiri http://blog.varunajayasiri.com

#OneOf

    Mod.require 'Models.Properties',
     'Models.Property.Base'
     'Models.Models'
     'Weya.Base'
     'Weya'
     (PROPERTIES, Base, MODELS, WeyaBase, Weya) ->

Class

      class OneOf extends Base
       @extend()

       propertyType: 'oneof'

       @default 'oneof', ['Null']
       @default 'default', ->
        return null if @schema.oneof.length is 0
        model = new (MODELS.get @schema.oneof[0])
        r = model.parse @schema.defaultValues.call this
        return r.value

       @default 'defaultValues', -> {}

       @default 'isDefault', (value) ->
        return true if @schema.oneof.length is 0
        model = new (MODELS.get @schema.oneof[0])
        model.parse value
        model.isDefault()

       @isValidSchema: (schema) ->
        return false if not super schema
        return false if not schema.oneof?
        return true

       parse: (data) ->
        r = super data
        return r unless r is true

        res =
         score: 0
         errors: ['invalid']
         value: @schema.default.call this
        for type in @schema.oneof
         m = new (MODELS.get type)
         r = m.parse data
         if res.score < r.score
          res = r

        return res

       toJSON: (value) -> value.toJSON()
       toJSONFull: (value) -> value.toJSONFull()

       edit: (elem, value, changed) ->
        new Edit this, elem, value, changed

Edit

      class Edit extends WeyaBase
       @extend()

       @initialize (property, elem, value, changed) ->
        @property = property
        @elems =
         parent: elem
        @model = value
        @onChanged = changed
        @render()

       render: ->
        Weya elem: @elems.parent, context: this, ->
         @div ".oneof-controls", ->
          @$.elems.type = @select on: {change: @$.on.typeChange}, ->
           for type in @$.property.schema.oneof
            @option value: type, type

         @$.elems.model = @div ".model", null

        #@elems.add.addEventListener 'click', @addClicked.bind this

        @renderModel()

       @listen 'typeChange', (e) ->
        type = @elems.type.value
        return if type is @model.type
        json = @model.toJSON()
        @model = new (MODELS.get type)
        r = @model.parse json
        @onChanged @model, true
        @renderModel()

       renderModel: ->
        @elems.model.innerHTML = ''
        @elems.type.value = @model.type
        @model.edit @elems.model, @modelChanged.bind this

       modelChanged: (value) ->
        @onChanged value, false

      PROPERTIES.register 'oneof', OneOf


update constructor docs

update constructor docs


'routes/' now is optional

'routes/' now is optional

# App

    'use strict'

    utils = require 'src/utils'
    log = require 'src/log'
    signal = require 'src/signal'
    db = require 'src/db'
    assert = require 'src/assert'
    Schema = require 'src/schema'
    Networking = require 'src/networking'
    Document = require 'src/document'
    Renderer = require 'src/renderer'
    Styles = require 'src/styles'
    Resources = require 'src/resources'
    Dict = require 'src/dict'

    AppRoute = require './route'

    if utils.isNode
        bootstrapRoute = require './bootstrap/route.node'

    `//<development>`
    pkg = require 'package.json'
    `//</development>`

    BASE_FILE_NAME_RE = /(.+)\.(?:node|server|client|browser|ios|android|native)/
    DEFAULT_CONFIG =
        title: 'Neft.io Application'
        protocol: 'http'
        port: 3000
        host: 'localhost'
        language: 'en'
        type: 'app'

    exports = module.exports = (opts = {}, extraOpts = {}) ->
        # Welcome log also for release mode
        log.ok "Welcome! Neft.io v#{pkg.version}; Feedback appreciated"

        `//<development>`
        log.warn "Use this bundle only in development; type --release when it's ready"
        `//</development>`

        config = utils.merge utils.clone(DEFAULT_CONFIG), opts.config

        if extraOpts?
            utils.merge config, extraOpts

# *Dict* app

        app = new Dict

## *Object* app.config = `{}`

Config object from the *package.json* file.

Can be overriden in the *init.js* file.

### type

The `app` type (the default one) uses renderer on the client side.

The `game` type uses special renderer (if exists) focused on more performance goals.

The `text` type always return HTML document with no renderer on the client side.
It's used for the crawlers (e.g. GoogleBot) or browsers with no javascript support.

```javascript
// package.json
{
    "name": "neft.io app",
    "version": "0.1.0",
    "config": {
        "title": "My first application!",
        "protocol": "http",
        "port": 3000,
        "host": "localhost",
        "language": "en",
        "type": "app"
    }
}
// init.js
module.exports = function(NeftApp) {
    var app = NeftApp({ title: "Overridden title" });
    console.log(app.config);
    // {title: "My first application!", protocol: "http", port: ....}
};
```

        app.config = config

## *Networking* app.networking

Standard Networking instance used to communicate
with the server and to create local requests.

All routes created by the *App.Route* uses this networking.

HTTP protocol is used by default with the data specified in the *package.json*.

        app.networking = new Networking
            type: Networking.HTTP
            protocol: config.protocol
            port: parseInt(config.port, 10)
            host: config.host
            url: config.url
            language: config.language

## *Object* app.models = `{}`

Files from the *models* folder with objects returned by their exported functions.

```javascript
// models/user/permission.js
module.exports = function(app) {
    return {
        getPermission: function(id){}
    };
};
// controllers/user.js
module.exports = function(app) {
    return {
        get: function(req, res, callback) {
            var data = app.models['user/permission'].getPermission(req.params.userId);
            callback(null, data);
        }
    }
};
```

        app.models = {}

## *Object* app.routes = `{}`

Files from the *routes* folder with objects returned by their exported functions.

        app.routes = {}

## *Object* app.styles = `{}`

Files from the *styles* folder as *Function*s
ready to create new *Item*s.

        app.styles = {}

## *Object* app.views = `{}`

Files from the *views* folder as the *Document* instances.

        app.views = {}

## *Resources* app.resources

        app.resources = do ->
            if opts.resources
                Resources.fromJSON(opts.resources)
            else
                new Resources

## *Signal* app.onReady()

Called when all modules, views, styled etc. have been loaded.

        signal.create app, 'onReady'

        # config.type
        config.type ?= 'app'
        assert.ok utils.has(['app', 'game', 'text'], config.type), "Unexpected app.config.type value. Accepted app/game/text, but '#{config.type}' got."

        app.Route = AppRoute app

## *Dict* app.cookies

On the client side, this object refers to the last received cookies
from the networking request.

On the server side, this cookies object are added into the each networking response.

By default, client has *clientId* and *sessionId* hashes.

```javascript
app.cookies.onChange(function(key){
    console.log('cookie changed', key, this[key]);
});
```

```xml
<h1>Your clientId</h1>
<em>${root.app.cookies.clientId}</em>
```

        # cookies
        COOKIES_KEY = '__neft_cookies'
        app.cookies = null
        onCookiesReady = (dict) ->
            app.cookies = dict
            if utils.isClient
                dict.set 'sessionId', utils.uid(16)
        db.get COOKIES_KEY, db.OBSERVABLE, (err, dict) ->
            if dict
                onCookiesReady dict
            else
                if utils.isClient
                    cookies = {clientId: utils.uid(16)}
                else
                    cookies = {}
                db.set COOKIES_KEY, cookies, (err) ->
                    db.get COOKIES_KEY, db.OBSERVABLE, (err, dict) ->
                        onCookiesReady dict
        app.networking.onRequest (req, res) ->
            if utils.isClient
                utils.merge req.cookies, app.cookies
            else
                utils.merge res.cookies, app.cookies
            req.onLoadEnd.listeners.unshift ->
                if utils.isClient
                    for key, val of res.cookies
                        unless utils.isEqual(app.cookies[key], val)
                            app.cookies.set key, val
                return
            , null
            return

        # propagate data
        Renderer.resources = app.resources
        Renderer.serverUrl = app.networking.url
        Document.Scripts.scripts = utils.arrayToObject opts.scripts,
            (index, elem) -> elem.name,
            (index, elem) -> elem.file

        # set styles window item
        if opts.styles?
            for style in opts.styles
                if style.name in ['view', '__view__']
                    style.file._init app: app, view: null
                    windowStyle = style.file._main.getComponent()
                    break

        windowStyleItem = windowStyle?.item or new Renderer.Item
        Renderer.window = windowStyleItem

        if opts.styles?
            stylesInitObject =
                app: app
                view: windowStyleItem

            # initialize styles
            for style in opts.styles when style.name?
                if style.name isnt 'view'
                    style.file._init stylesInitObject
                app.styles[style.name] = style.file

        # load styles
        Styles
            windowStyle: windowStyle
            styles: app.styles
            queries: opts.styleQueries
            resources: app.resources

        # load bootstrap
        if utils.isNode
            bootstrapRoute app

        # loading files helper
        init = (files, target) ->
            for file in files when file.name?
                if typeof file.file isnt 'function'
                    continue

                fileObj = file.file app
                target[file.name] = fileObj

                if baseNameMatch = BASE_FILE_NAME_RE.exec(file.name)
                    [_, baseName] = baseNameMatch
                    if target[baseName]?
                        if utils.isPlainObject(target[baseName]) and utils.isPlainObject(fileObj)
                            fileObj = utils.merge Object.create(target[baseName]), fileObj
                    target[baseName] = fileObj

            return

        # load document extensions
        if utils.isObject(opts.extensions)
            for ext in opts.extensions
                ext app: app

        # exports app classes
        exports.app =
            Route: app.Route

        # load views
        for view in opts.views when view.name?
            app.views[view.name] = Document.fromJSON view.file

        # load files
        init opts.models, app.models
        init opts.routes, app.routes

        for path, obj of app.routes
            r = {}
            if utils.isObject(obj) and not (obj instanceof app.Route)
                for method, opts of obj
                    if utils.isObject(opts)
                        route = new app.Route method, opts
                        r[route.name] = route
                    else
                        r[method] = opts
            app.routes[path] = r

        # provide default index route
        unless app.routes.index
            app.routes.index = new app.Route 'get /', {}

        # emit ready signal
        app.onReady.emit()

        app

# App

    'use strict'

    utils = require 'src/utils'
    log = require 'src/log'
    signal = require 'src/signal'
    db = require 'src/db'
    assert = require 'src/assert'
    Schema = require 'src/schema'
    Networking = require 'src/networking'
    Document = require 'src/document'
    Renderer = require 'src/renderer'
    Styles = require 'src/styles'
    Resources = require 'src/resources'
    Dict = require 'src/dict'

    AppRoute = require './route'

    if utils.isNode
        bootstrapRoute = require './bootstrap/route.node'

    `//<development>`
    pkg = require 'package.json'
    `//</development>`

    BASE_FILE_NAME_RE = /(.+)\.(?:node|server|client|browser|ios|android|native)/
    DEFAULT_CONFIG =
        title: 'Neft.io Application'
        protocol: 'http'
        port: 3000
        host: 'localhost'
        language: 'en'
        type: 'app'

    exports = module.exports = (opts = {}, extraOpts = {}) ->
        # Welcome log also for release mode
        log.ok "Welcome! Neft.io v#{pkg.version}; Feedback appreciated"

        `//<development>`
        log.warn "Use this bundle only in development; type --release when it's ready"
        `//</development>`

        config = utils.merge utils.clone(DEFAULT_CONFIG), opts.config

        if extraOpts?
            utils.merge config, extraOpts

# *Dict* app

        app = new Dict

## *Object* app.config = `{}`

Config object from the *package.json* file.

Can be overriden in the *init.js* file.

### type

The `app` type (the default one) uses renderer on the client side.

The `game` type uses special renderer (if exists) focused on more performance goals.

The `text` type always return HTML document with no renderer on the client side.
It's used for the crawlers (e.g. GoogleBot) or browsers with no javascript support.

```javascript
// package.json
{
    "name": "neft.io app",
    "version": "0.1.0",
    "config": {
        "title": "My first application!",
        "protocol": "http",
        "port": 3000,
        "host": "localhost",
        "language": "en",
        "type": "app"
    }
}
// init.js
module.exports = function(NeftApp) {
    var app = NeftApp({ title: "Overridden title" });
    console.log(app.config);
    // {title: "My first application!", protocol: "http", port: ....}
};
```

        app.config = config

## *Networking* app.networking

Standard Networking instance used to communicate
with the server and to create local requests.

All routes created by the *App.Route* uses this networking.

HTTP protocol is used by default with the data specified in the *package.json*.

        app.networking = new Networking
            type: Networking.HTTP
            protocol: config.protocol
            port: parseInt(config.port, 10)
            host: config.host
            url: config.url
            language: config.language

## *Object* app.models = `{}`

Files from the *models* folder with objects returned by their exported functions.

```javascript
// models/user/permission.js
module.exports = function(app) {
    return {
        getPermission: function(id){}
    };
};
// controllers/user.js
module.exports = function(app) {
    return {
        get: function(req, res, callback) {
            var data = app.models['user/permission'].getPermission(req.params.userId);
            callback(null, data);
        }
    }
};
```

        app.models = {}

## *Object* app.routes = `{}`

Files from the *routes* folder with objects returned by their exported functions.

        app.routes = {}

## *Object* app.styles = `{}`

Files from the *styles* folder as *Function*s
ready to create new *Item*s.

        app.styles = {}

## *Object* app.views = `{}`

Files from the *views* folder as the *Document* instances.

        app.views = {}

## *Resources* app.resources

        app.resources = do ->
            if opts.resources
                Resources.fromJSON(opts.resources)
            else
                new Resources

## *Signal* app.onReady()

Called when all modules, views, styled etc. have been loaded.

        signal.create app, 'onReady'

        # config.type
        config.type ?= 'app'
        assert.ok utils.has(['app', 'game', 'text'], config.type), "Unexpected app.config.type value. Accepted app/game/text, but '#{config.type}' got."

        app.Route = AppRoute app

## *Dict* app.cookies

On the client side, this object refers to the last received cookies
from the networking request.

On the server side, this cookies object are added into the each networking response.

By default, client has *clientId* and *sessionId* hashes.

```javascript
app.cookies.onChange(function(key){
    console.log('cookie changed', key, this[key]);
});
```

```xml
<h1>Your clientId</h1>
<em>${root.app.cookies.clientId}</em>
```

        # cookies
        COOKIES_KEY = '__neft_cookies'
        app.cookies = null
        onCookiesReady = (dict) ->
            app.cookies = dict
            if utils.isClient
                dict.set 'sessionId', utils.uid(16)
        db.get COOKIES_KEY, db.OBSERVABLE, (err, dict) ->
            if dict
                onCookiesReady dict
            else
                if utils.isClient
                    cookies = {clientId: utils.uid(16)}
                else
                    cookies = {}
                db.set COOKIES_KEY, cookies, (err) ->
                    db.get COOKIES_KEY, db.OBSERVABLE, (err, dict) ->
                        onCookiesReady dict
        app.networking.onRequest (req, res) ->
            if utils.isClient
                utils.merge req.cookies, app.cookies
            else
                utils.merge res.cookies, app.cookies
            req.onLoadEnd.listeners.unshift ->
                if utils.isClient
                    for key, val of res.cookies
                        unless utils.isEqual(app.cookies[key], val)
                            app.cookies.set key, val
                return
            , null
            return

        # propagate data
        Renderer.resources = app.resources
        Renderer.serverUrl = app.networking.url
        Document.Scripts.scripts = utils.arrayToObject opts.scripts,
            (index, elem) -> elem.name,
            (index, elem) -> elem.file

        # set styles window item
        if opts.styles?
            for style in opts.styles
                if style.name in ['view', '__view__']
                    style.file._init app: app, view: null
                    windowStyle = style.file._main.getComponent()
                    break

        windowStyleItem = windowStyle?.item or new Renderer.Item
        Renderer.window = windowStyleItem

        if opts.styles?
            stylesInitObject =
                app: app
                view: windowStyleItem

            # initialize styles
            for style in opts.styles when style.name?
                if style.name isnt 'view'
                    style.file._init stylesInitObject
                app.styles[style.name] = style.file

        # load styles
        Styles
            windowStyle: windowStyle
            styles: app.styles
            queries: opts.styleQueries
            resources: app.resources

        # load bootstrap
        if utils.isNode
            bootstrapRoute app

        # loading files helper
        init = (files, target) ->
            for file in files when file.name?
                if typeof file.file isnt 'function'
                    continue

                fileObj = file.file app
                target[file.name] = fileObj

                if baseNameMatch = BASE_FILE_NAME_RE.exec(file.name)
                    [_, baseName] = baseNameMatch
                    if target[baseName]?
                        if utils.isPlainObject(target[baseName]) and utils.isPlainObject(fileObj)
                            fileObj = utils.merge Object.create(target[baseName]), fileObj
                    target[baseName] = fileObj

            return

        # load document extensions
        if utils.isObject(opts.extensions)
            for ext in opts.extensions
                ext app: app

        # exports app classes
        exports.app =
            Route: app.Route

        # load views
        for view in opts.views when view.name?
            app.views[view.name] = Document.fromJSON view.file

        # load files
        init opts.models, app.models
        init opts.routes, app.routes

        for path, obj of app.routes
            r = {}
            if utils.isObject(obj) and not (obj instanceof app.Route)
                for method, opts of obj
                    if utils.isObject(opts)
                        route = new app.Route method, opts
                        r[route.name] = route
                    else
                        r[method] = opts
            app.routes[path] = r

        # emit ready signal
        app.onReady.emit()

        app


Use @warn instead of Madul.FIRE directly

    Array.prototype.last = -> this[this.length - 1]

    q     = require 'q'
    fs    = require 'fs'
    async = require 'async'
    uuid  = require 'uuid-1345'

    microtime = require 'microtime'

    EventEmitter2 = require('eventemitter2').EventEmitter2
    emitter = new EventEmitter2 wildcard: true

    available   = { }
    initialized = { }

    code_root = process.env.MADUL_ROOT || 'dist'

    underscore = (value) -> value.replace /\W+/g, '_'

    WRAPPED = { }

    _INITIALIZERS = { }

    INITIALIZERS = (cls, method) ->
      _INITIALIZERS[cls] = [ ] unless _INITIALIZERS[cls]?

      _INITIALIZERS[cls].push method

    WRAP = (cls, method) ->
      unless WRAPPED[cls]?
        WRAPPED[cls] = [
          'constructor'
          'listen'
          'fire'
          'warn'
          'report'
          'do_wrap'
          'wrap_methods'
          'strip'
          'make_available'
          'hydrate'
          'finish_up'
          'initialize'
        ]

      WRAPPED[cls].indexOf(method) < 0

    class Madul

      @LISTEN: (event, callback) => emitter.on event, callback

      @FIRE: (event, args) => emitter.emit event, args

      listen: (event, callback) => Madul.LISTEN "*.#{@clazz}.#{event}", callback

      fire: (event, args) => Madul.FIRE "@.#{@clazz}.#{event}", args

      warn: (event, details) => Madul.FIRE "!.#{@clazz}.#{event}", details

      report: (method, state, id, args) =>
        @fire "#{method}.#{state}",
          uuid:      id
          args:      args
          timestamp: microtime.now()

      do_wrap: (method) =>
        callMe = @[method]

        Madul.FIRE "$.#{@clazz}.#{method}.wrap"

        @["_#{method}"] = @[method]

        respond = (id, deferred) =>
          (state, data) =>
            process.nextTick =>
              deferred[state] data

              @report method, state, id, data

        WRAPPED[@clazz].push method

        @[method] = =>
          id   = uuid.v4fast()
          def  = q.defer()
          args = if arguments[0]? then Array.prototype.slice.call arguments else [ ]
          res  = respond id, def

          args.push (out) => res 'resolve', out
          args.push (err) => res 'reject',  err
          args.push (out) => res 'notify',  out

          @report method, 'invoke', id, args

          @["_#{method}"].apply @, args

          def.promise

      wrap_methods: =>
        props = Object.keys @constructor.prototype

        for prop in props
          if prop[0] != '_'                      &&
             typeof @[prop] == 'function'        &&
             typeof @["_#{prop}"] == 'undefined' &&
             WRAP(@clazz, prop) == true

            @do_wrap prop

            INITIALIZERS @clazz, prop if prop[0] == '$'

      strip: (name) => name.toLowerCase().replace /\W+/g, ''

      make_available: (name, mod) =>
        Madul.FIRE "$.#{name}.available"

        available[@strip name] = mod

      hydrate: (deps, done) =>
        load = (name, path, loaded) =>
          mod = require path

          if typeof mod == 'function'
            new mod name
              .initialize @ctor_params?[name]
              .then (mod) =>
                @make_available name, mod
                loaded()
          else
            @make_available name, mod
            loaded()

        check = (path, dep, finished) =>
          fs.readdir path, (err, files) =>
            if err?
              @warn 'file-not-found', err

              done type: 'ERROR', info: err
            else
              async.each files, (f, next) =>
                depth = "#{path}/#{f}"

                fs.stat depth, (e, s) =>
                  if s.isDirectory() && f[0] != '.'
                    check depth, dep, next
                  else if s.isFile() && f.substring(0, f.length - 3) == dep
                    load f.substring(0, f.length - 3), depth, => do_add dep, next
                  else
                    next()
              , finished

        add_dependency = (name, mod) =>
          Madul.FIRE "$.#{@clazz}.dependency.register", name
          @[underscore name] = mod

        do_add = (name, next) =>
          add_dependency name, available[@strip name]
          next()

        add = (name, resource, next) =>
          @make_available name, require resource
          do_add name, next

        async.each deps, (d, next) =>
          if @[underscore d]? == false
            if available[@strip d]? == true
              do_add d, next
            else
              try
                add d, d, next
              catch e
                cwd = process.cwd()
                pkg = "#{cwd}/node_modules/#{d}/package.json"

                fs.stat pkg, (err, stat) =>
                  if stat?.isFile()
                    fs.readFile pkg, 'utf8', (err, data) =>
                      json = JSON.parse data
                      main = json.main || json._main || 'index.js'

                      add d, "#{cwd}/node_modules/#{d}/#{main}", next
                  else
                    check "#{cwd}/#{code_root}", d, next
          else
            next()
        , done

      finish_up: (resolve, args) =>
        async.each _INITIALIZERS[@clazz], (initializer, next) =>
          @[initializer] args
            .then next
        , =>
          @make_available @clazz, @
          resolve @
          Madul.FIRE "$.#{@clazz}.initialized"

      initialize: =>
        deferred = q.defer()
        args     = if arguments[0]? then Array.prototype.slice.call arguments else undefined

        @clazz = @constructor.name unless @clazz?

        Madul.FIRE "$.#{@clazz}.request_instance", args

        if initialized[@clazz] != true
          Madul.FIRE "$.#{@clazz}.initialize", args

          initialized[@clazz] = true

          @wrap_methods()

          if @deps?
            @hydrate @deps, => @finish_up deferred.resolve, args
          else
            @finish_up deferred.resolve, args
        else
          process.nextTick => deferred.resolve available[@strip @clazz]

        deferred.promise

    module.exports = Madul

    Array.prototype.last = -> this[this.length - 1]

    q     = require 'q'
    fs    = require 'fs'
    async = require 'async'
    uuid  = require 'uuid-1345'

    microtime = require 'microtime'

    EventEmitter2 = require('eventemitter2').EventEmitter2
    emitter = new EventEmitter2 wildcard: true

    available   = { }
    initialized = { }

    code_root = process.env.MADUL_ROOT || 'dist'

    underscore = (value) -> value.replace /\W+/g, '_'

    WRAPPED = { }

    _INITIALIZERS = { }

    INITIALIZERS = (cls, method) ->
      _INITIALIZERS[cls] = [ ] unless _INITIALIZERS[cls]?

      _INITIALIZERS[cls].push method

    WRAP = (cls, method) ->
      unless WRAPPED[cls]?
        WRAPPED[cls] = [
          'constructor'
          'listen'
          'fire'
          'warn'
          'report'
          'do_wrap'
          'wrap_methods'
          'strip'
          'make_available'
          'hydrate'
          'finish_up'
          'initialize'
        ]

      WRAPPED[cls].indexOf(method) < 0

    class Madul

      @LISTEN: (event, callback) => emitter.on event, callback

      @FIRE: (event, args) => emitter.emit event, args

      listen: (event, callback) => Madul.LISTEN "*.#{@clazz}.#{event}", callback

      fire: (event, args) => Madul.FIRE "@.#{@clazz}.#{event}", args

      warn: (event, details) => Madul.FIRE "!.#{@clazz}.#{event}", details

      report: (method, state, id, args) =>
        @fire "#{method}.#{state}",
          uuid:      id
          args:      args
          timestamp: microtime.now()

      do_wrap: (method) =>
        callMe = @[method]

        Madul.FIRE "$.#{@clazz}.#{method}.wrap"

        @["_#{method}"] = @[method]

        respond = (id, deferred) =>
          (state, data) =>
            process.nextTick =>
              deferred[state] data

              @report method, state, id, data

        WRAPPED[@clazz].push method

        @[method] = =>
          id   = uuid.v4fast()
          def  = q.defer()
          args = if arguments[0]? then Array.prototype.slice.call arguments else [ ]
          res  = respond id, def

          args.push (out) => res 'resolve', out
          args.push (err) => res 'reject',  err
          args.push (out) => res 'notify',  out

          @report method, 'invoke', id, args

          @["_#{method}"].apply @, args

          def.promise

      wrap_methods: =>
        props = Object.keys @constructor.prototype

        for prop in props
          if prop[0] != '_'                      &&
             typeof @[prop] == 'function'        &&
             typeof @["_#{prop}"] == 'undefined' &&
             WRAP(@clazz, prop) == true

            @do_wrap prop

            INITIALIZERS @clazz, prop if prop[0] == '$'

      strip: (name) => name.toLowerCase().replace /\W+/g, ''

      make_available: (name, mod) =>
        Madul.FIRE "$.#{name}.available"

        available[@strip name] = mod

      hydrate: (deps, done) =>
        load = (name, path, loaded) =>
          mod = require path

          if typeof mod == 'function'
            new mod name
              .initialize @ctor_params?[name]
              .then (mod) =>
                @make_available name, mod
                loaded()
          else
            @make_available name, mod
            loaded()

        check = (path, dep, finished) =>
          fs.readdir path, (err, files) =>
            if err?
              Madul.FIRE '$.error', err

              done type: 'ERROR', info: err
            else
              async.each files, (f, next) =>
                depth = "#{path}/#{f}"

                fs.stat depth, (e, s) =>
                  if s.isDirectory() && f[0] != '.'
                    check depth, dep, next
                  else if s.isFile() && f.substring(0, f.length - 3) == dep
                    load f.substring(0, f.length - 3), depth, => do_add dep, next
                  else
                    next()
              , finished

        add_dependency = (name, mod) =>
          Madul.FIRE "$.#{@clazz}.dependency.register", name
          @[underscore name] = mod

        do_add = (name, next) =>
          add_dependency name, available[@strip name]
          next()

        add = (name, resource, next) =>
          @make_available name, require resource
          do_add name, next

        async.each deps, (d, next) =>
          if @[underscore d]? == false
            if available[@strip d]? == true
              do_add d, next
            else
              try
                add d, d, next
              catch e
                cwd = process.cwd()
                pkg = "#{cwd}/node_modules/#{d}/package.json"

                fs.stat pkg, (err, stat) =>
                  if stat?.isFile()
                    fs.readFile pkg, 'utf8', (err, data) =>
                      json = JSON.parse data
                      main = json.main || json._main || 'index.js'

                      add d, "#{cwd}/node_modules/#{d}/#{main}", next
                  else
                    check "#{cwd}/#{code_root}", d, next
          else
            next()
        , done

      finish_up: (resolve, args) =>
        async.each _INITIALIZERS[@clazz], (initializer, next) =>
          @[initializer] args
            .then next
        , =>
          @make_available @clazz, @
          resolve @
          Madul.FIRE "$.#{@clazz}.initialized"

      initialize: =>
        deferred = q.defer()
        args     = if arguments[0]? then Array.prototype.slice.call arguments else undefined

        @clazz = @constructor.name unless @clazz?

        Madul.FIRE "$.#{@clazz}.request_instance", args

        if initialized[@clazz] != true
          Madul.FIRE "$.#{@clazz}.initialize", args

          initialized[@clazz] = true

          @wrap_methods()

          if @deps?
            @hydrate @deps, => @finish_up deferred.resolve, args
          else
            @finish_up deferred.resolve, args
        else
          process.nextTick => deferred.resolve available[@strip @clazz]

        deferred.promise

    module.exports = Madul