webmcp
view framework/env/request/add_variable.lua @ 259:41be09ce6aa3
Allow tables as sub-values of request variables (won't be cloned)
| author | jbe | 
|---|---|
| date | Sun Mar 15 01:14:21 2015 +0100 (2015-03-15) | 
| parents | 9e4be058959d | 
| children | 
 line source
     1 --[[--
     2 request.add_variable(
     3   tbl,                 -- table where the variable is stored
     4   key,                 -- name of variable (key within the table)
     5   value                -- optional value for initialization
     6 )
     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. If the (modified) value is a table, the table will be cloned on every request to avoid side-effects between different requests. Note, however, that only the first level of the table is cloned. See env/request/__init.lua for examples.
    10 --]]--
    12 function request.add_variable(tbl, key, value)
    13   local initval, istable
    14   tbl[key] = value
    15   request.add_initializer(function(first)
    16     if first then
    17       initval = tbl[key]
    18       istable = type(initval) == "table"
    19     else
    20       if istable then
    21         tbl[key] = table.new(initval)
    22       else
    23         tbl[key] = initval
    24       end
    25     end
    26   end)
    27 end
