-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForeach.cls
More file actions
74 lines (71 loc) · 1.83 KB
/
Foreach.cls
File metadata and controls
74 lines (71 loc) · 1.83 KB
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
Class User.Foreach
{
// TODO: Add support for multi-dimensional arrays
// Example of how to use the foreach functionality
ClassMethod SampleMethod() as %Status {
try {
// Troubleshooting vars for dynamic objects and arrays, activate as needed
set message = {}
set message.sidjfdf = "Dynamic 1"
set message.sdjfs = ""
set message.sdiofsoi = "Dynamic 2"
/*
set message(1) = "thing 1"
set message(2) = "thing 2"
*/
// Pass the object, the function we want run, and our class location to the foreach
do ..Foreach(.message, "Action", $this)
}
catch(e) {
write "SampleMethod error" _e.Name
}
return $$$OK
}
// A method that is called on each iteration
ClassMethod Action(pMes) as %Status {
try {
write pMes, !
}
catch(e) {
write "Action error " _e.Name
}
return $$$OK
}
ClassMethod Foreach(ByRef pVal, pFunc, pClass) As %Status {
try {
// We get the key value pairs from our dynamic object
set tIter = pVal.%GetIterator()
if tIter = "" throw
// Loop through these values and add to our query depending on their contents
while tIter.%GetNext(.key, .value) {
// We pass the value to the action
// TODO: Add ability to capture value and key in a friendly way
do $classmethod(pClass, pFunc, value)
//write key_"='"_value_"'",!
}
while tIter.%GetNext(.key, .value) {
// We pass the value to the action
// TODO: Add ability to capture value and key in a friendly way
do $classmethod(pClass, pFunc, value)
//write key_"='"_value_"'",!
}
}
catch (e) {
try {
// Element position init
set tStruct=""
// Loop through the array
for {
set tStruct=$order(pVal(tStruct))
quit:tStruct=""
// Perform our action method on each element
do $classmethod(pClass, pFunc, pVal(tStruct))
}
}
catch (e) {
write "Foreach error " _e.Name
}
}
return $$$OK
}
}