public
Created

lokki

  • Download Gist
lokki.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
// Lokki.js 0.2.0
// (c) 2011 Micheal
 
// Load Important Libs
var FS = require('fs')
, INFLECTION = require('./inflection')
, Express = require('express')
, App = Express.createServer();
// Constants
var RESOURCE_URLS = ["index","new","create","edit","update","destroy","show"],
APP_DIR = process.cwd();
 
// Lokki Var
var Lokki = {};
 
// Current Version
Lokki.VERSION = '0.2.0';
 
// Lokki Controllers
Lokki.Controllers = [];
// Lokki Routes
Lokki.Routes = {};
 
// Lokki.Route
// --------------
// Create New Route
Lokki.Route = function() {
this._controller = "";
this._action = "";
this._url = "";
Lokki.Routes.push(this);
}
 
// Root
Lokki.Route.prototype.add = function(type, route) {
if (route.indexOf("#") === -1) throw new Error("root url is not defined properly");
var self = this,
temp = route.split("#"),
controller = temp[0],
action = temp[1];
// Assign Controller
self._controller = controller;
// Assign Action
self._action = action;
// Assign Route
self._url = "/";
// create Express Routes
switch(type) {
case 'root':
break;
}
}
 
// Lokki.Controller
// --------------
// Create New Controller
Lokki.Controller = function(name, opts) {
this._controller = name;
this._filters = opts.filters; // Get Filter Function if present
this._actions = opts.actions || {}; // Get Actions
// Initialize Controller
this.initialize();
// Will be used internally
this._filters_for_actions = opts.actions.keys().toObject() || {};
}
 
// Initialize New Controller
Lokki.Controller.prototype.initialize = function() {
var self = this;
// Execute Filters
if (typeof this._filters != 'undefined')
this._filters();
}
 
Lokki.Controller.prototype.apply_before_filter = function(fn, opts) {
// Lokki
var self = this,
actions = self._actions.keys();
// Error Handling
// No function Exists
if (typeof fn === 'undefined') throw new Error("function is not defined");
// Cannot Define Both Except and Only Constraints for filter
if (opts && opts.except && opts.only) throw new Error("cannot define both except and only");
// Convert Constraint Data to Array, if string
// and then populate relevent filter functions
// to associated action
if(opts) {
if (opts.except) {
if (typeof opts.except === "string") opts.except = opts.except.split(",");
// Run across all actions except the above and push the function as before filter
actions.map(function(action) {
if (opts.except.indexOf(action) === -1) {
self._filters_for_actions[action].push(fn);
}
});
} else if (opts.only) {
if (typeof opts.only === "string") opts.only = opts.only.split(",");
// Run across only particular actions and push the function as before filter
opts.only.map(function(action) {
self._filters_for_actions[action].push(fn);
});
}
} else {
// Run across all actions and push the function as before filter
actions.map(function(action) {
self._filters_for_actions[action].push(fn);
});
}
}
 
/**
* Boots the Controller
*/
function bootControllers() {
// Default controller path
var controller_path = APP_DIR + "/controllers/";
// Boot Each Controller
try {
var files = FS.readdirSync(controller_path);
// If no controllers are found
if (files.length === 1) return false;
// Load Application Controller First
require(controller_path + "application");
// Load rest of the controller
files.forEach(function(file){
file = file.replace('.js', '');
console.log('loading controller =>', file);
if (file != "application") require(controller_path + file);
});
} catch (e) {
console.log("===> Boot Controller ==>", e.message);
}
}
 
/**
* Boots the Lib Files from Lib Folder
*/
function bootLibs() {
// Default Lib path
var lib_path = APP_DIR + "/libs/";
// Boot Each Lib
try {
var files = FS.readdirSync(lib_path);
// If no Lib are found
if (files.length === 1) return false;
// Load rest of the controller
files.forEach(function(file){
file = file.replace('.js', '');
console.log('loading Lib =>', file);
require(lib_path + file);
});
} catch (e) {
console.log("===> Boot LIBS ==>", e.message);
}
}
 
// Lokki Public Methods
Lokki.public = {
Controller: Lokki.Controller
, Init: function(port,cb) {
console.log("Running Lokki => ", Lokki.VERSION);
// Boot Libs
bootLibs();
// Boot Controllers
bootControllers();
// CallBack if any
if (typeof cb != "undefined") cb();
console.log("Server Listening at port ", port);
// Start Server and listen on PORT
App.listen(port);
}
}
 
// The top-level namespace. All public Lokki classes and modules will
// be attached to this. Exported for Nodejs.
// GLOBAL
GLOBAL["Lokki"] = {
Express: Express
, App: merge(App,Lokki.public)
, Middleware: {}
, Vars: {}
}
 
module.exports = GLOBAL["Lokki"];
 
// UTIL
//-----
// Merge Object with another Object
function merge(a, b){
var keys = Object.keys(b);
for (var i = 0, len = keys.length; i < len; ++i) {
var key = keys[i];
a[key] = b[key]
}
return a;
};
 
// Get Keys of Object
Object.prototype.keys = function() {
var o = this
, acc = [];
for (var propertyName in o) {
if(o.hasOwnProperty(propertyName)) acc.push(propertyName);
}
return acc;
}
 
// Converts Array to Object
Array.prototype.toObject = function() {
var o = this
acc = {};
for (var i = 0, len = o.length; i < len; i++) {
acc[o[i]] = [];
}
return acc;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.