Skip to content

Commit 2a4da65

Browse files
authored
feat: add a property builder method to classes
2 parents 4a3fe03 + 5415077 commit 2a4da65

File tree

1 file changed

+65
-26
lines changed

1 file changed

+65
-26
lines changed

class_loader.lua

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@ end
2525

2626
local ClassLoader = {}
2727

28+
do
29+
local type = type
30+
local getmetatable = getmetatable
31+
32+
function ClassLoader.EnhancedType(object)
33+
local typ = type(object)
34+
35+
if typ == "table" then
36+
local objectType = object.__type
37+
if objectType == "EnumValue" then
38+
local declaringClass = object._declaringClass
39+
if declaringClass then
40+
typ = declaringClass.__name or typ
41+
end
42+
elseif objectType == "Enum" then
43+
typ = object.__name .. "#Template"
44+
elseif getmetatable(object) == nil then --This is an uninitialised class.
45+
typ = object.__name and (object.__name .. "#Template") or typ
46+
else
47+
typ = object.__name or typ
48+
end
49+
end
50+
51+
return typ
52+
end
53+
end
54+
2855
local packages = {}
2956
local currentPackage, currentFile
3057
local baseDirectory
@@ -109,7 +136,7 @@ do
109136
end
110137

111138
local function instantiateClass(class)
112-
assert(class, "The class constructor must be called with a colon")
139+
assert(istable(class), "The class constructor must be called with a colon")
113140
return setmetatable({}, class)
114141
end
115142

@@ -139,6 +166,7 @@ do
139166

140167
class.New = instantiateClass
141168
class.Extends = ClassLoader.ExtendObject
169+
class.Property = ClassLoader.CreateProperty
142170

143171
return class
144172
end,
@@ -154,30 +182,6 @@ do
154182
end
155183
}
156184

157-
local type = type
158-
local getmetatable = getmetatable
159-
local function enhancedType(object)
160-
local typ = type(object)
161-
162-
if typ == "table" then
163-
local objectType = object.__type
164-
if objectType == "EnumValue" then
165-
local declaringClass = object._declaringClass
166-
if declaringClass then
167-
typ = declaringClass.__name or typ
168-
end
169-
elseif objectType == "Enum" then
170-
typ = object.__name .. "#Template"
171-
elseif getmetatable(object) == nil then --This is an uninitialised class.
172-
typ = object.__name and (object.__name .. "#Template") or typ
173-
else
174-
typ = object.__name or typ
175-
end
176-
end
177-
178-
return typ
179-
end
180-
181185
baseEnvironment.__index = baseEnvironment
182186
setmetatable(baseEnvironment, {__index = _G})
183187

@@ -193,7 +197,7 @@ do
193197
__type = "Package",
194198
__isPackage = true,
195199
_G = _G,
196-
type = enhancedType,
200+
type = ClassLoader.EnhancedType,
197201
pairs = pairs,
198202
ipairs = ipairs
199203
}
@@ -222,6 +226,37 @@ function ClassLoader.ExtendObject(class, super)
222226
return class
223227
end
224228

229+
function ClassLoader.CreateProperty(class, name, typ, default)
230+
assert(istable(class), "The property builder must be called with a colon")
231+
assert(isstring(name), "The property name must be a string")
232+
assert(typ == nil or isstring(typ), "The property type must be a string or nil")
233+
234+
local privateName = "_" .. name:gsub("^%u", string.lower)
235+
236+
class["Get" .. name] = function(self)
237+
return self[privateName]
238+
end
239+
240+
if typ then
241+
class["Set" .. name] = function(self, value)
242+
local actualType = ClassLoader.EnhancedType(value)
243+
assert(actualType == typ, "Unexpected value of type \"" .. actualType .. "\" given for property: " .. name)
244+
245+
self[privateName] = value
246+
end
247+
else
248+
class["Set" .. name] = function(self, value)
249+
self[privateName] = value
250+
end
251+
end
252+
253+
if default then
254+
class["Set" .. name](class, default)
255+
end
256+
257+
return class
258+
end
259+
225260
do
226261
local function filePathToObjectPackageName(filePath)
227262
return filePath
@@ -329,6 +364,10 @@ do
329364
object.Extends = nil
330365
end
331366

367+
if object.Property == ClassLoader.CreateProperty then
368+
object.Property = nil
369+
end
370+
332371
if object.__type == "Enum" then
333372
setupEnum(object)
334373
end

0 commit comments

Comments
 (0)