local module = {} setmetatable(module, {__index=_G}) setfenv(1, module) --===== Components =====-- Component = {} function Component.newFactory(name, params, serialize) local metatable = {} metatable.__tostring = serialize or Component.serialize metatable.__newindex = function(tbl, index, value) if params[index] ~= nil then tbl[index] = value else error(string.format("%q is not a valid key for a component of type %q", index, name)) end end return function(tbl) local tbl = tbl or {} local self = { __type=name } for k, v in pairs(params) do self[k] = v end setmetatable(self, metatable) for k, v in pairs(tbl) do self[k] = v end return self end end function Component.serialize(self) local str = "{" for k, v in pairs(self) do if type(v) == "string" then str = str .. string.format("%s=%q,", k, v) else str = str .. string.format("%s=%s,", k, tostring(v)) end end str = string.sub(str, 1, -2) .. "}" return str end return module