-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEnumGenerator.ring
105 lines (82 loc) · 2.09 KB
/
EnumGenerator.ring
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Enum generator
# Mohammad Hamdy Ghanem
# 30/10/2020
# ====== Usage Sample =========
x = GenerateEnum("Aliignment", [
:Left = 1,
:Right = 2,
:Center = 3])
? x
# ==============================
func GenerateEnum(Name, lstEnumValues)
if not isList(lstEnumValues)
raise("Bad parameter type!")
elseif len(lstEnumValues) = 0
raise("Enum must contain members")
end
#================template================================
enumTemp = "
%EnumName% = new %EnumName%Enum
class %EnumName%Enum
%members%
func GetName(value)
switch value
%cases% else
return NULL
end
func GetValues()
return [%values%]
func GetNames()
return [%names%]
func ToList()
return [%list%]
private
%ReadOnly%
"
#============eval=================
members = ""
cases = ""
names = ""
values = ""
list = ""
readOnly = ""
if type(lstEnumValues[1]) != "LIST"
lstEnumValues = FixEnumList(lstEnumValues)
end
For v in lstEnumValues
value=""
if type(v[2]) = "NUMBER"
value = v[2]
else
value = '"' + v[2] + '"'
end
member = v[1] + " = " + value
members += " " + member + nl
readOnly += " Func Set" + v[1] + "()" + nl
cases += " case " + value + nl +
" return " + v[1] + nl
if len(Names) > 0
Names += ", "
values += ", "
list += ", "
end
Names += V[1]
values += value
list += ":" + member
Next
enum = subStr(enumTemp, "%EnumName%", Name)
enum = subStr(enum, "%members%", members)
enum = subStr(enum, "%cases%", cases)
enum = subStr(enum, "%names%", names)
enum = subStr(enum, "%values%", values)
enum = subStr(enum, "%list%", list)
enum = subStr(enum, "%ReadOnly%", readOnly)
return enum
func FixEnumList(aList)
aEnum = []
nCounter = 1
for item in aList
aEnum[item] = nCounter
nCounter++
next
return aEnum