Skip to content

Commit 06ef41d

Browse files
authored
Added support for dynamic objects and arrays (one dimension). Cannot detect which is which accurately yet.
1 parent 29b2d80 commit 06ef41d

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

Foreach.cls

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ Class User.Foreach
55
// Example of how to use the foreach functionality
66
ClassMethod SampleMethod() as %Status {
77
try {
8-
set message(1) = "Hey there"
9-
set message(2) = "Hey thereeee"
10-
do ..Foreach($this, "Action", .message)
8+
//set message = {}
9+
set message(1) = "thing 1"
10+
set message(2) = "thing 2"
11+
do ..Foreach(.message, "Action", $this)
1112
}
1213
catch(e) {
1314
write "SampleMethod error" _e.Name
@@ -26,8 +27,41 @@ ClassMethod Action(pMes) as %Status {
2627
return $$$OK
2728
}
2829

30+
ClassMethod Foreach(ByRef pVal, pFunc, pClass) As %Status {
31+
try {
32+
// We get the key value pairs from our dynamic object
33+
set tIter = pVal.%GetIterator()
34+
// Loop through these values and add to our query depending on their contents
35+
while tIter.%GetNext(.key, .value) {
36+
if value '= "" {
37+
// We pass the value to the action
38+
// TODO: Add ability to capture value and key in a friendly way
39+
do $classmethod(pClass, pFunc, value)
40+
//write key_"='"_value_"'",!
41+
}
42+
}
43+
}
44+
catch (e) {
45+
try {
46+
// Element position init
47+
set tStruct=""
48+
// Loop through the array
49+
for {
50+
set tStruct=$order(pVal(tStruct))
51+
quit:tStruct=""
52+
// Perform our action method on each element
53+
do $classmethod(pClass, pFunc, pVal(tStruct))
54+
}
55+
}
56+
catch (e) {
57+
write "Foreach error " _e.Name
58+
}
59+
}
60+
return $$$OK
61+
}
62+
2963
// Iteration over an object
30-
ClassMethod Foreach(pClass, pFunc, ByRef pVal) As %Status {
64+
ClassMethod Foreachs(ByRef pVal, pFunc, pClass) As %Status {
3165
try {
3266
// Element position init
3367
set tStruct=""
@@ -36,7 +70,7 @@ ClassMethod Foreach(pClass, pFunc, ByRef pVal) As %Status {
3670
set tStruct=$order(pVal(tStruct))
3771
quit:tStruct=""
3872
// Perform our action method on each element
39-
do $CLASSMETHOD(pClass, pFunc, pVal(tStruct))
73+
do $classmethod(pClass, pFunc, pVal(tStruct))
4074
}
4175
}
4276
catch (e) {

0 commit comments

Comments
 (0)