webmcp
diff framework/env/request/add_variable.lua @ 255:9e4be058959d
New functions request.add_initializer(...) and request.add_variable(...) to allow per-request initialization; Merged request.process() with request.handler(...)
author | jbe |
---|---|
date | Sat Mar 14 23:39:47 2015 +0100 (2015-03-14) |
parents | |
children | 41be09ce6aa3 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/request/add_variable.lua Sat Mar 14 23:39:47 2015 +0100 1.3 @@ -0,0 +1,22 @@ 1.4 +--[[-- 1.5 +request.add_variable( 1.6 + tbl, -- table where the variable is stored 1.7 + key, -- name of variable (key within the table) 1.8 + value -- optional value for initialization 1.9 +) 1.10 + 1.11 +Marks a field of a table to be re-initialized for every request. If this variable (i.e. the field of the table) is modified before the first requst is being handled (e.g. during configuration or pre-/post-fork initializers), then the modified value will be used for re-initialization on every request. See env/request/__init.lua for an example. 1.12 + 1.13 +--]]-- 1.14 + 1.15 +function request.add_variable(tbl, key, value) 1.16 + local initialized_value 1.17 + tbl[key] = value 1.18 + request.add_initializer(function(first) 1.19 + if first then 1.20 + initialized_value = tbl[key] 1.21 + else 1.22 + tbl[key] = initialized_value 1.23 + end 1.24 + end) 1.25 +end