'use strict';

var path = require('path'),
    glob = require('glob');

/**
 * RequirementsMapper constructor
 * @param {String}       dir          directory to map
 * @param {String|Array} globPattern  glob pattern(s)
 */
function RequirementsMapper(dir, globPattern) {
    this.dir = dir || '.';
    this.globPattern = globPattern || '**/*.js?(on)';
    this.cwd = path.isAbsolute(this.dir) ? this.dir : path.join(path.dirname(module.parent.filename), this.dir);
    this.globOptions = {};
}

/**
 * create requirements object
 * @return {Object}  plain object containing required matches
 */
RequirementsMapper.prototype.map = function() {
    this.globOptions.cwd = this.cwd;
    return glob.sync(this.globPattern, this.globOptions).reduce(function(obj, file) {
            this.node = obj;
            this.file = file;
            this.filePath = path.normalize(file).split(path.sep);
            this.filePath.filter(function(node) {
                return !!node;
            }).forEach(nestNodes.bind(this));
            return obj;
        }.bind(this), {});
};

/**
 * iterate through path nodes and nest them in map object
 * @param  {String}  nodeName  name of the current node
 * @param  {Integer} i         node names iterator
 */
function nestNodes(nodeName, i) {
    if (this.filePath.length - 1 > i) {
        if (!this.node.hasOwnProperty(nodeName) || !isPlainObject(this.node[nodeName])) {
            this.node[nodeName] = {};
        }
        this.node = this.node[nodeName];
    } else {
        this.node[path.basename(nodeName, path.extname(nodeName))] = require(path.join(this.cwd, this.file));
    }
}

/**
 * test if given object is a plain object
 * @param  {mixed}   obj  object to test
 * @return {Boolean}      result
 */
function isPlainObject(obj) {
    return typeof obj === 'object' && obj.constructor === Object;
}

/**
 * export class
 * @type {RequirementsMapper}
 */
module.exports = RequirementsMapper;

// necessary to get the current `module.parent` and resolve paths correctly
delete require.cache[__filename];

@@ -65,16 +65,17 @@
 lob');%0A%0A
+%0A
 /**%0A * R
@@ -485,24 +485,25 @@
 ns = %7B%7D;%0A%7D%0A%0A
+%0A
 /**%0A * creat
@@ -1068,16 +1068,17 @@
 %7D);%0A%7D;%0A%0A
+%0A
 /**%0A * i
@@ -1623,24 +1623,25 @@
 );%0A    %7D%0A%7D%0A%0A
+%0A
 /**%0A * test 
@@ -1851,16 +1851,17 @@
 ect;%0A%7D%0A%0A
+%0A
 /**%0A * e
@@ -1906,16 +1906,16 @@
 er%7D%0A */%0A
-
 module.e
@@ -1944,16 +1944,17 @@
 apper;%0A%0A
+%0A
 // neces

add some whitespace