diff options
Diffstat (limited to 'lua/mvc.lua')
-rwxr-xr-x | lua/mvc.lua | 68 |
1 files changed, 34 insertions, 34 deletions
diff --git a/lua/mvc.lua b/lua/mvc.lua index efed01f..0026020 100755 --- a/lua/mvc.lua +++ b/lua/mvc.lua @@ -1,4 +1,4 @@ ---[[ Basic MVC framework +--[[ Basic MVC framework Written for Alpine Configuration Framework (ACF) see www.alpinelinux.org for more information Copyright (C) 2007 Nathan Angelacos @@ -20,7 +20,7 @@ mymodule.mvc = {} -- the constructor --[[ Builds a new MVC object. If "module" is given, then tries to load self.conf.appdir .. module "-controller.lua" in c.worker and - self.conf.appdir .. module "-model.lua" in c.model + self.conf.appdir .. module "-model.lua" in c.model The returned .conf table is guaranteed to have the following appdir - where the application lives @@ -35,16 +35,16 @@ mymodule.new = function (self, modname) local c = {} c.worker = {} c.model = {} - + -- make defaults if the parent doesn't have them if self.conf == nil then - c.conf = { appdir = "", confdir = "", + c.conf = { appdir = "", confdir = "", tempdir = "", appname = "" } end -- If no clientdata, then clientdata is a null table - if self.clientdata == nil then - c.clientdata = {} + if self.clientdata == nil then + c.clientdata = {} end -- If we don't have an application name, use the modname @@ -54,51 +54,51 @@ mymodule.new = function (self, modname) -- load the module code here if (modname) then - c.worker = self:soft_require( modname .. "-controller") + c.worker = self:soft_require( modname .. "-controller") if c.worker == nil then c.worker = {} worker_loaded = false end - c.model = self:soft_require( modname .. "-model" ) + c.model = self:soft_require( modname .. "-model" ) if c.model == nil then c.model = {} model_loaded = false end end - -- The magic that makes all the metatables point in the correct - -- direction. c.model -> c.worker -> parent -> parent.worker -> + -- The magic that makes all the metatables point in the correct + -- direction. c.model -> c.worker -> parent -> parent.worker -> -- grandparent -> grandparent -> worker (and so on) - - -- The model looks in worker for missing + + -- The model looks in worker for missing setmetatable (c.model, c.model ) c.model.__index = c.worker - -- the worker looks in the parent table for missing + -- the worker looks in the parent table for missing setmetatable (c.worker, c.worker) c.worker.__index = self - + -- the table looks in the worker for missing setmetatable (c, c) c.__index = c.worker - + -- ensure an "mvc" table exists, even if empty if (type(rawget(c.worker, "mvc")) ~= "table") then c.worker.mvc = {} end - + setmetatable (c.worker.mvc, c.worker.mvc) - -- If creating a new parent container, then + -- If creating a new parent container, then -- we are the top of the chain. if (modname) then c.worker.mvc.__index = self.worker.mvc else c.worker.mvc.__index = self.mvc - end - + end + -- run the worker on_load code if type(rawget(c.worker.mvc, "on_load")) == "function" then - c.worker.mvc.on_load(c, self) + c.worker.mvc.on_load(c, self) c.worker.mvc.on_load = nil end @@ -130,10 +130,10 @@ mymodule.destroy = function (self) if self.model and self.model["_NAME"] then package.loaded[self.model["_NAME"]] = nil end end --- This is a sample front controller/dispatch. -mymodule.dispatch = function (self, userprefix, userctlr, useraction, clientdata) +-- This is a sample front controller/dispatch. +mymodule.dispatch = function (self, userprefix, userctlr, useraction, clientdata) local controller = nil - local success, err = xpcall ( function () + local success, err = xpcall ( function () self.conf.prefix = userprefix or "/" self.conf.controller = userctlr or "" self.conf.action = useraction or "" @@ -153,7 +153,7 @@ mymodule.dispatch = function (self, userprefix, userctlr, useraction, clientdata local action = controller.conf.action - -- Because of the inheritance, normally the + -- Because of the inheritance, normally the -- controller.worker.action will flow up, so that all children have -- actions of all parents. We use rawget to make sure that only -- controller defined actions are used on dispatch @@ -163,13 +163,13 @@ mymodule.dispatch = function (self, userprefix, userctlr, useraction, clientdata error (self.conf) end - -- run the (first found) pre_exec code, starting at the controller + -- run the (first found) pre_exec code, starting at the controller -- and moving up the parents if type(controller.worker.mvc.pre_exec) == "function" then controller.worker.mvc.pre_exec ( controller ) end - -- run the action + -- run the action local viewtable = controller.worker[action](controller) -- run the post_exec code @@ -186,20 +186,20 @@ mymodule.dispatch = function (self, userprefix, userctlr, useraction, clientdata local viewfunc, p1, p2, p3 = controller:view_resolver() viewfunc (viewtable, p1, p2, p3) end - + -- we're done with the controller, destroy it controller:destroy() controller = nil return viewtable - end, + end, self:soft_traceback(message) ) if not success then local handler - if controller then + if controller then handler = controller.worker or controller if handler then handler:exception_handler(err) end controller:destroy() @@ -222,7 +222,7 @@ mymodule.soft_require = function (self, name ) if file then file:close() local PATH=package.path - -- FIXME - this should really try to open the lua file, + -- FIXME - this should really try to open the lua file, -- and if it doesnt exist silently fail. -- This version allows things from /usr/local/lua/5.1 to -- be loaded @@ -270,7 +270,7 @@ mymodule.read_config = function( self, appname, home ) if self.conf.libdir then package.path = string.gsub(self.conf.libdir, ",", "/?.lua;") .. "/?.lua;" .. package.path end - + if (#self.conf.confdir) then -- check for an appname-hooks.lua file self.conf.app_hooks = {} setmetatable (self.conf.app_hooks, {__index = _G}) @@ -295,7 +295,7 @@ end -- parse a "URI" like string into a prefix, controller and action -- return them (or blank strings) mymodule.parse_path_info = function( str ) - str = str or "" + str = str or "" local words = {} str = string.gsub(str, "/+$", "") for x=1,3 do @@ -373,7 +373,7 @@ mymodule.view_resolver = function(self) if viewname then func = haserl.loadfile (viewname) end - + -- create the view helper library viewlibrary = self:create_helper_library() @@ -389,7 +389,7 @@ mymodule.view_resolver = function(self) orig_action = self.conf.orig_action or self.conf.prefix .. self.conf.controller .. "/" .. self.conf.action, clientdata = self.clientdata, } - + return func, viewlibrary, pageinfo, self.sessiondata end |