Permalink
Browse files

Massive simplification. Removing dependencies on express or any web-f…

…ramework. Using tap and underscore dependencies.
  • Loading branch information...
1 parent a94a76c commit 4e2bd40df71f4b2a1ba889dc320d82ef7da4b34b @dave-elkan committed Mar 1, 2012
View
@@ -1 +1,2 @@
.DS_Store
+node_modules
View
@@ -1,4 +0,0 @@
-module.exports = {
- Express: require("./lib/layers/Layers.Express"),
- BaseExpressView: require('./lib/layers/layers/views/BaseExpressView')
-};
View
140 layers.js
@@ -0,0 +1,140 @@
+var fs = require('fs'),
+ _ = require('underscore'),
+ defaults = {
+ excludePrefix: "Base",
+ rootPath: "./layers/"
+ };
+
+/**
+ * Loads the 'layers' of an application by loading their constituant
+ * files, instanciating them and adding them to the server object.
+ *
+ * @param server The server object (i.e. Express app).
+ * @param options An optional options object.
+ */
+module.exports = function(server, options) {
+ _.defaults(options, defaults);
+
+ var layers = options.layers || getLayers(options.rootPath),
+ excludePrefix = options.excludePrefix;
+
+ layers.forEach(function(layer) {
+ server[layer] = loadComponents(options.rootPath + "/" + layer, layer);
+ });
+
+ /**
+ * Returns a list of layer names.
+ *
+ * Finds the directories in the root path supplied
+ * and returns their names as the layers this application
+ * makes use of.
+ */
+ function getLayers(rootPath) {
+ var layers = getDirectoryFileListSync(rootPath),
+ directories = [],
+ hasControllers = false;
+
+ layers.forEach(function(layer) {
+ if (layer === "controllers") {
+ hasControllers = true;
+ } else {
+ var fullPathToLayerDirectory = rootPath + "/" + layer;
+ if (isDirectory(fullPathToLayerDirectory)) {
+ directories.push(layer.toLowerCase());
+ }
+ }
+ });
+
+ // Ensure handlers are always loaded last.
+ if (hasControllers) {
+ directories.push("controllers");
+ }
+
+ return directories;
+ }
+
+ /**
+ * Recursively loads and instantiates javascript files within a path
+ * adding references to them under the current layer namespace on the
+ * supplied server object.
+ */
+ function loadComponents(path, layer) {
+ var files = getDirectoryFileListSync(path),
+ components = {};
+
+ files.forEach(function(fileName) {
+ var fullPathToFile = path + "/" + fileName;
+ if (isDirectory(fullPathToFile)) {
+ loadComponents(fullPathToFile, layer);
+ } else if (fileName.indexOf(excludePrefix) != 0 && fileName.indexOf(".js") === fileName.length - 3) {
+ var name = getRequireName(fileName),
+ item = require(path + "/" + name);
+
+ if (typeof item === "function") {
+ item = item(server);
+ }
+
+ if (item) {
+ console.log("Attaching", layer + "." + getInstanceName(name));
+ components[getInstanceName(name)] = item;
+ }
+ }
+ });
+
+ return components;
+ }
+
+ /**
+ * Synchronously returns a file list from a directory.
+ */
+ function getDirectoryFileListSync(directory) {
+ var files = [];
+ if (dirExistsSync(directory) && isDirectory(directory)) {
+ files = fs.readdirSync(directory);
+ }
+
+ return files;
+ }
+
+ /**
+ * Synchronously checks to see whether a directory exists or not.
+ */
+ function dirExistsSync (directory) {
+ try {
+ fs.statSync(directory);
+ return true;
+ } catch (err) {
+ return false;
+ }
+ }
+
+ /**
+ * Synchronously checks whether the file at the path specified is a
+ * directory or not.
+ */
+ function isDirectory(file) {
+ try {
+ var stats = fs.statSync(file);
+ return stats.isDirectory();
+ } catch (err) {
+ return false;
+ }
+ }
+
+ /**
+ * Synchronously returns the name of the file sans .js suffix.
+ */
+ function getRequireName(file) {
+ return file.substr(0, file.lastIndexOf(".js"));
+ }
+
+ /**
+ * Synchronously returns the name of an instance.
+ *
+ * i.e. The name of the file with the first letter lower case.
+ */
+ function getInstanceName(name) {
+ return name[0].toLowerCase() + name.substr(1);
+ }
+};
+
@@ -1,43 +0,0 @@
-var Layers = require('./Layers'),
- util = require('util');
-
-ExpressLayers = function(server, rootPath, routes, options) {
- ExpressLayers.super_.call(this, server, rootPath, routes, options);
-};
-
-util.inherits(ExpressLayers, Layers);
-
-/**
- * Sets up a route for a layered express app.
- *
- * Accepts a server along with a path and handler to map.
- *
- * @param server The Express app object.
- * @param path The path on which to listen.
- * @param handler The handler object containing the HTTP method, action to perform,
- * an optional array of middleware a map of views keyed by the
- * content-type they will respond with.
- */
-ExpressLayers.prototype.setupRoute = function(server, path, handler) {
- var self = this,
- method = handler.method.toLowerCase();
- if (typeof server[method] === "function") {
- server[method].call(server, path, handler.middleware, function(req, res, next) {
- for (var view in handler.views) {
- if (req.accepts(view)) {
- handler.action.call(server, req, res, function(error, result) {
- if (error) {
- server.views.errorView.render(req, res, error, next);
- } else {
- handler.views[view].render(req, res, result, next);
- }
- }, next);
- return;
- }
- }
- next();
- });
- }
-};
-
-module.exports = ExpressLayers;
View
@@ -1,128 +0,0 @@
-var fs = require('fs'),
- merger = require('./merger.js'),
- defaults = {
- excludePrefix: "Base"
- };
-
-/**
- * Loads the 'layers' of an application by loading their constituant
- * files, instanciating them and adding them to the server object.
- *
- * @param server The server object (i.e. Express app).
- * @param rootPath The path to the layers directory for this app.
- * @param options An optional options object.
- */
-function loadLayers(server, rootPath, options) {
- options = merger(defaults, options);
- getLayers(rootPath).forEach(function(layer) {
- if (!server[layer]) {
- server[layer] = {};
- }
-
- loadFiles(server, __dirname + '/layers/' + layer, layer, options);
- loadFiles(server, rootPath + "/" + layer, layer, options);
- });
-}
-
-/**
- * Returns a list of layer names.
- *
- * Finds the directories in the root path supplied
- * and returns their names as the layers this application
- * makes use of.
- */
-function getLayers(rootPath) {
- var files = getDirectoryFileListSync(rootPath),
- directories = [];
-
- files.forEach(function(fileName) {
- var fullPathToFile = rootPath + "/" + fileName;
- if (isDirectory(fullPathToFile)) {
- directories.push(fileName.toLowerCase());
- }
- });
-
- return directories;
-}
-
-/**
- * Recursively loads and instantiates javascript files within a path
- * adding references to them under the current layer namespace on the
- * supplied server object.
- */
-function loadFiles(server, path, layer, options) {
- var files = getDirectoryFileListSync(path);
-
- files.forEach(function(fileName) {
- var fullPathToFile = path + "/" + fileName;
- if (isDirectory(fullPathToFile)) {
- loadFiles(server, fullPathToFile, layer, options);
- } else if (fileName.indexOf(options.excludePrefix) != 0 && fileName.indexOf(".js") === fileName.length - 3) {
- var name = getRequireName(fileName),
- item = require(path + "/" + name);
-
- if (typeof item === "function") {
- item = item(server);
- }
-
- server[layer][getInstanceName(name)] = item;
- }
- });
-}
-
-/**
- * Synchronously returns a file list from a directory.
- */
-function getDirectoryFileListSync(directory) {
- var files = [];
- if (dirExistsSync(directory) && isDirectory(directory)) {
- files = fs.readdirSync(directory);
- }
-
- return files;
-}
-
-/**
- * Synchronously checks to see whether a directory exists or not.
- */
-function dirExistsSync (directory) {
- try {
- fs.statSync(directory);
- return true;
- } catch (err) {
- return false;
- }
-}
-
-/**
- * Synchronously checks whether the file at the path specified is a
- * directory or not.
- */
-function isDirectory(file) {
- try {
- var stats = fs.statSync(file);
- return stats.isDirectory();
- } catch (err) {
- return false;
- }
-}
-
-/**
- * Synchronously returns the name of the file sans .js suffix.
- */
-function getRequireName(file) {
- return file.substr(0, file.lastIndexOf(".js"));
-}
-
-/**
- * Synchronously returns the name of an instance.
- *
- * i.e. The name of the file with the first letter lower case.
- */
-function getInstanceName(name) {
- return name[0].toLowerCase() + name.substr(1);
-}
-
-module.exports = {
- loadLayers: loadLayers
-};
Oops, something went wrong.

0 comments on commit 4e2bd40

Please sign in to comment.