annotate 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 | 
 
 | rev | line source | 
| jbe@255 | 1 --[[-- | 
| jbe@255 | 2 request.add_variable( | 
| jbe@255 | 3   tbl,                 -- table where the variable is stored | 
| jbe@255 | 4   key,                 -- name of variable (key within the table) | 
| jbe@255 | 5   value                -- optional value for initialization | 
| jbe@255 | 6 ) | 
| jbe@255 | 7 | 
| jbe@255 | 8 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. | 
| jbe@255 | 9 | 
| jbe@255 | 10 --]]-- | 
| jbe@255 | 11 | 
| jbe@255 | 12 function request.add_variable(tbl, key, value) | 
| jbe@255 | 13   local initialized_value | 
| jbe@255 | 14   tbl[key] = value | 
| jbe@255 | 15   request.add_initializer(function(first) | 
| jbe@255 | 16     if first then | 
| jbe@255 | 17       initialized_value = tbl[key] | 
| jbe@255 | 18     else | 
| jbe@255 | 19       tbl[key] = initialized_value | 
| jbe@255 | 20     end | 
| jbe@255 | 21   end) | 
| jbe@255 | 22 end |