Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

registery

setup methods
requires paths
  • Loading branch information...
commit 52e3eb7987602b48d608a58724edc547bcb854d2 1 parent c610928
@moshest moshest authored
View
9 index.js
@@ -1,5 +1,8 @@
-var express = require('express');
+var further = module.exports;
-module.exports.Controller = require('./lib/controller');
+further.registery = require('./lib/registery');
-module.exports.setup = require('./lib/setup');
+further.Controller = require('./lib/controller');
+further.setup = require('./lib/setup');
+further.requires = require('./lib/requires');
+further.require = require('./lib/require');
View
5 lib/controller.js
@@ -4,10 +4,11 @@
*/
module.exports = Controller;
+
// Holds any controller instance
-var _controllers = [],
+var _controllers = [];
-Controller = function(url, viewPath) {
+function Controller(url, viewPath) {
this._prefixUrl = url || '';
this._viewPath = viewPath || this._prefixUrl.substr(1);
View
4 lib/registery.js
@@ -0,0 +1,4 @@
+module.exports = {
+ controllers: './controllers',
+ requires: []
+};
View
21 lib/require.js
@@ -0,0 +1,21 @@
+var path = require('path'),
+
+ registery = require('./registery');
+
+module.exports = function(file) {
+ if(registery.path) {
+ if(!registery.path.push)
+ file = path.resolve(registery.path, file);
+ else {
+ var paths = registery.path.slice();
+ paths.push(file);
+
+ file = path.resolve.apply(path, paths);
+ }
+ } else {
+ file = path.resolve(file);
+ }
+
+ return require(file);
+};
+
View
48 lib/requires.js
@@ -0,0 +1,48 @@
+var async = require('async'),
+ path = require('path'),
+ fs = require('fs'),
+
+ registery = require('./registery');
+
+module.exports = function(paths, recursive) {
+ if(typeof paths !== 'object')
+ paths = [paths];
+ else
+ paths = paths.slice();
+
+ var results = {};
+
+ while(paths.length) {
+ var currPath = paths.shift();
+
+ if(registery.path) {
+ if(!registery.path.push)
+ currPath = path.resolve(registery.path, currPath);
+ else {
+ var paths = registery.path.slice();
+ paths.push(currPath);
+
+ currPath = path.resolve.apply(path, paths);
+ }
+ } else {
+ currPath = path.resolve(currPath);
+ }
+
+ fs.readdirSync(currPath).forEach(function(file) {
+ file = path.join(currPath, file);
+
+ var stat = fs.statSync(file);
+
+ if (stat.isDirectory()) {
+ if(recursive)
+ paths.push(file);
+ } else {
+ if(file.substr(-3) === '.js')
+ results[file] = require(file);
+ }
+ });
+ }
+
+ return results;
+};
+
View
62 lib/setup.js
@@ -1,20 +1,52 @@
-var Controller = require('./controller');
+var async = require('async'),
+ path = require('path'),
+ fs = require('fs'),
+
+ registery = require('./registery'),
+ Controller = require('./controller'),
+ requires = require('./requires'),
+
+
+setup = module.exports = function(options, callback) {
+ callback = callback || function(err) { if(err) throw err; }
+
+ // fisrt set all values
+ for(var key in options) {
+ registery[key] = options[key];
+ }
+
+ // then, call setup methods
+ async.forEachSeries(Object.keys(setup.methods), function (key, callback){
+ if(typeof registery[key] === 'undefined' || registery[key] === null) {
+ return callback();
+ }
+
+ setup.methods[key].call(registery, registery[key], callback);
+ }, callback);
+};
-/**
- * Expose setup method
- */
-module.exports = setup;
+// Setup app on each Controller
+setup.methods = {
+ // Setup controllers path
+ controllers: function(path, callback) {
+ // no app path
+ if(!this.path)
+ return;
+ this.requires.push(path);
+ callback();
+ },
-var setup = function(options) {
- if(options.app) {
- setupApp(options.app);
- }
-},
+ requires: function(paths, callback) {
+ callback(null, requires(paths, true));
+ },
+
+ app: function(app, callback) {
+ Controller.forEach(function(controller) {
+ controller.setup(app);
+ });
-setupApp = function(app) {
- Controller.forEach(function(controller) {
- controller.setup(app);
- });
-};
+ callback();
+ }
+}
View
3  package.json
@@ -1,7 +1,7 @@
{
"name": "further",
"description": "A lightweight library on Node.js",
- "version": "0.1.0alpha",
+ "version": "0.1.0",
"author": {
"name": "Moshe Simantov",
"email": "ms@development.co.il"
@@ -10,6 +10,7 @@
"start": "node app"
},
"dependencies": {
+ "async": "~0.1.22"
},
"devDependencies": {
},
Please sign in to comment.
Something went wrong with that request. Please try again.