var os             = require('os')
  , fs             = require('fs')
  , path           = require('path')
  , util           = require('util')
  , getModulePaths = require(path.resolve(path.join(__dirname, 'getModulePaths')))
  , packageJson    = require(path.resolve(path.join(__dirname, '..', '..', 'package.json')))
  , isWin          = /^win32/.test(os.platform())
  , underscore     = require('underscore')
  , debug          = require('debug')('cleverstack:utils:helpers');

module.exports = {

  /**
   * Makes sure that the NODE_PATH is correctly set in the current process so that child processes
   * have access to cleverstack modules as well as magic modules like require('config')
   * 
   * @return {String} Returns the path joined by the correct delimiter for the operating system
   */
  nodePath: function() {
    var delimiter    = isWin ? ';' : ':'
      , currentPaths = process.env.NODE_PATH ? process.env.NODE_PATH.split(delimiter) : []
      , appRoot      = path.resolve(path.join(__dirname, '..', '..'))
      , paths        = [ path.join(appRoot, 'lib'), path.join(appRoot, 'modules') ]
      , nodePath;

    currentPaths.forEach(function(_path) {
      if (!/lib(\/|\\)?$|modules(\/|\\)?$/.test(_path)) {
        paths.push(_path);
      }
    });

    nodePath = paths.join(delimiter);
    if (debug.enabled) {
      debug(util.format('NODE_PATH=%s', nodePath));
    }

    return nodePath;
  },

  /**
   * Helper function that will load a file based on its name from every enabled module in your application
   * 
   * @param  {String}   fileName Name of the file you want to load
   * @param  {Object}   config   Applications config object
   * @param  {Object}   cluster  NodeJS Cluster module
   * @param  {Function} debug  Function that can be called with debugging information
   * 
   * @return {null}
   */
  loadModulesFileByName: function(fileName, config, cluster, debug) {
    getModulePaths().forEach(function(modulePath) {
      var file = path.resolve(path.join(modulePath, 'bin', fileName));
      if (fs.existsSync(file)) {
        if (debug.enabled) {
          debug('Loading ' + fileName + ' from ' + modulePath.split(path.sep).pop());
        }
        require(file)(cluster, config, packageJson, debug);
      }
    });
  },

  getFilesForFolder: function(folderName) {
    var files = [];

    if (fs.existsSync(folderName)) {
      fs.readdirSync(folderName).forEach(function(fileName) {
        if (fs.statSync(path.join(folderName, fileName)).isFile() && /\.js$/.test(fileName)) {
          files.push(fileName);
        }
      });
    }

    return files;
  },

  /**
   * Get the name of the file at the given offset to be used as a ClassName
   * 
   * @param  {Number}         offset the offset in the stack
   * @return {String|Boolean} the filename or false if we can't manage to get the filename
   */
  getClassName: function(offset) {
    var Reg         = new RegExp('.*\\(([^\\)]+)\\:.*\\:.*\\)', 'ig')
      , stack       = new Error().stack.split('\n')
      , file        = stack.splice(offset, 1);

    return Reg.test(file) ? RegExp.$1.split(path.sep).pop().replace('.js', '') : false;
  },

  defineProperty: function(obj, prop, options) {
    var defaults = {
      writable     : false,
      enumerable   : false,
      configurable : false
    };

    if (typeof options === 'function') {
      options = {
        get: options
      };
      delete defaults.writable;
    }

    // debug(util.format('Defining %s with options of %s', prop, util.inspect(options, {showHidden: false, colors: true, customInspect: true, depth: 0})));

    options = underscore.extend(defaults, options);

    Object.defineProperty(obj, prop, options);
  },

  debugInspect: function(obj) {
    return util.inspect(obj, {showHidden: false, colors: true, customInspect: true, depth: 1}).replace(/\n[\ ]+/igm, ' ');
  }
};

@@ -3866,15 +3866,563 @@
 , ' ');%0A
+  %7D,%0A%0A  includeModel: function(findOptions, model, as, includes) %7B%0A    var include;%0A%0A    if (!findOptions.include) %7B%0A      findOptions.include = %5B%5D;%0A    %7D%0A    %0A    if (!(include = underscore.findWhere(findOptions.include, %7Bas: as%7D))) %7B%0A      include = %7B%0A        model : model.entity,%0A        as    : as%0A      %7D%0A      findOptions.include.push(include);%0A    %7D%0A%0A    if (!!includes) %7B%0A      if (!include.include) %7B%0A        include.include = includes;%0A      %7D else %7B%0A        underscore.extend(include.include, includes);%0A      %7D%0A    %7D%0A%0A    return this;%0A
   %7D%0A%7D;%0A

Include helper to generate includes quickly for queries