-
-
Save lyuz1n/6ef834507fbbeb57040ea5a325af6cbb to your computer and use it in GitHub Desktop.
Class Helper
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
local class = { | |
_VERSION = 'class.lua v2020.02.08', | |
_URL = 'https://gist.github.com/lyuz1n/6ef834507fbbeb57040ea5a325af6cbb' | |
} | |
local function copyTable(table) | |
local result = {} | |
local mt = getmetatable(table) | |
if mt then | |
setmetatable(result, mt) | |
end | |
for k, v in pairs(table) do | |
if type(v) == 'table' and k ~= '__index' and k ~= '__newindex' then | |
result[k] = copyTable(v) | |
else | |
result[k] = v | |
end | |
end | |
return result | |
end | |
setmetatable(class, { | |
__call = function(self, classScope) | |
return setmetatable({}, { | |
__index = classScope, | |
__call = function(self, objectScope) | |
local obj = objectScope or {} | |
setmetatable(obj, {__index = self}) | |
if obj.constructor and type(obj.constructor) == 'function' then | |
obj:constructor() | |
end | |
for key, value in pairs(classScope) do | |
if type(value) == 'table' then | |
obj[key] = copyTable(value) | |
else | |
obj[key] = value | |
end | |
end | |
return obj | |
end | |
}) | |
end | |
}) | |
return class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment