-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.lua
34 lines (30 loc) · 904 Bytes
/
module.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
---@class Module
---Module utility functions.
local Module = { __name = "Module" }
setmetatable(Module, Module)
local mt = { __index = Module }
---Create a new module.
---@param init table|string
---@return Module
function Module:__call(init)
local module = type(init) == "table" and init or {}
if type(init) == "string" then
module.__name = init
end
return setmetatable(module, mt)
end
--- Exports all the functions in `self` to the target `scope`. Skips keys which
--- start with an underscore and values where the key already exists on `scope`.
---@param scope table? defaults to `_G`, the global environment.
function Module:export(scope)
scope = scope or _G
for key, value in pairs(self) do
if key:byte() ~= 95 then
scope[key] = scope[key] or value
end
end
end
function Module:__tostring()
return self.__name
end
return Module