Created
September 29, 2022 21:36
-
-
Save michlbro/e91e8228250333e71b9d4d462c5339a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
USAGE: | |
local import = require(thisModule) | |
local class = import {"class",...} .from "path/to/class" | |
class -> {class = {}, ...} | |
]]-- | |
local loaded = {} | |
local importer = {} | |
function importer.from(self) | |
return function(a) | |
local loadedClasses = {} | |
local path = string.split(a, "/") | |
local parent = game | |
for _, target in path do | |
local child = parent:FindFirstChild(target) | |
if child then | |
parent = child | |
else | |
error(string.format("%s not a valid member of %s", target, parent.Name)) | |
end | |
end | |
for _, classTarget in self.classes do | |
local class = parent:FindFirstChild(classTarget) | |
if loaded[classTarget] then | |
loadedClasses[classTarget] = loaded[class] | |
end | |
if class and class.ClassName == "ModuleScript" then | |
local requiredClass = require(class) | |
loaded[classTarget] = requiredClass | |
loadedClasses[classTarget] = requiredClass | |
else | |
error(string.format("%s not a valid class in %s", classTarget, parent.Name)) | |
end | |
end | |
return loadedClasses | |
end | |
end | |
local function import(a) | |
if type(a) == "table" then | |
return setmetatable({classes = a},{__index = function(tbl, index) | |
return importer[index](tbl) | |
end}) | |
end | |
end | |
return import |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment