This method is quite useful when updating an existing object with attribute / 
values of a source object.

Why / when would you use this?
If you are, say, editing an OBJECT (or entity). You copied the object to an 
$editObj;  then afterwards want to conditionally update it and know what was 
changed
[This also has the feature of revealing exactly what was changed in the OBJECT 
so that you can use that information if useful…]


  // OB_UPDATE ( dstObj; srcObj ) --> ResultObj.  Conditionally updates the 
dstObj with attributes from srcObj [if any difference]. ResultObj gives 
feedback on what was updated
C_OBJECT($1;$2)  // dstObj; srcObj
C_OBJECT($0;$resultObj)  // resultObj. Will give the "result" and feedback on 
what was changed
$resultObj:=New object("result";False;"new";New collection;"update";New 
collection)
  // we set up the RESULT object to have a:
  // .result — TRUE if there were any changes made
  // .new [ ] — a collection with all the NEW attributes added to dstObj [from 
srcObj]
  // .update [ ] — a collection with all the attribute values UPDATED in dstObj 
[from srcObj]

For each ($attr;$2)  // go through all the attributes for the Source Object
        Case of 
                : (Undefined($1[$attr]))  // if the attribute does not already 
exist in destination, we will need to add it, and 'write this down' in the 
result object
                        $resultObj.new.push(New object($attr;$2[$attr]))  // 
remember we added this attribute
                        $resultObj.result:=True  // updates made
                        $1[$attr]:=$2[$attr]

                : ($1[$attr]#$2[$attr])  // change in the attribute
                        $resultObj.update.push(New 
object($attr;$2[$attr];"old";$1[$attr]))  // remember we added this attribute
                        $resultObj.result:=True  // updates made
                        $1[$attr]:=$2[$attr]

        End case 
End for each 

$0:=$resultObj  // the result!

—————————————

the result Object will be in this format:
.result  — TRUE if changes made; FALSE if nothing changed
.new – COLLECTION of new attributes added to dstObj from srcObj.     
.update — COLLECTION of attributes that received updated values.  It gives both 
the current value and the old one.

So, for example, if you try it with this tester code:

  // zzTEST_OB_UPDATE -- test OB_UPDATE( ) function
C_OBJECT($srcObj;$dstObj;$result)
$srcObj:=New object("name";"Chris";"address";"5619 Vanlerberg 
Road";"city";"Fernie";"age";55)
$dstObj:=New object("name";"Tracine";"address";"109 Ridgemont 
Crescent";"city";"Fernie")
$resultObj:=OB_UPDATE ($dstObj;$srcObj)

— $dstObj will be made to be identical to $srcObj; {because $dstObj does not 
possess any attributes not found in $srcObj}
— $resultObj will tell you what was changed and how
in this case:
$resultObj.result = TRUE
$result.new [ ]  has   [{age:55}]
$result.update has    [{name:Chris,old:Tracine},{address:5619 Vanlerberg 
Road,old:109 Ridgemont Crescent}]

— So not only have you updated $dstObj with all the values of $srcObj 
(including adding any missing attributes), you know what the changes are

—————————————

In the scenario when you have $dstObj having attributes lacking in $srcObj 
{i.e. $srcObj has only a SUBSET if the attributes} ...
  // zzTEST_OB_UPDATE -- test OB_UPDATE( ) function
C_OBJECT($srcObj;$dstObj;$result)
  // now when you update $dstObj from $srcObj, and $dstObj has attributes that 
are NOT in $srcObj, those attributes are not touched.
// so you can update $dstObj with just a subset of attributes using $srcObj
$dstObj:=New object("name";"Chris";"address";"5619 Vanlerberg 
Road";"city";"Fernie";"age";55)
$srcObj:=New object("name";"Tracine";"address";"109 Ridgemont 
Crescent";"city";"Fernie")

$resultObj:=OB_UPDATE ($dstObj;$srcObj)

$dstObj continues to have the “age”:55 attributes {which the $srcObj did not 
have}, but it also gets all the updates from $srcObj. So it is made identical 
to $srcObj EXCEPT FOR attributes of $dstObj that were not in $srcObj

— $dstObj becomes:   {name:Tracine,address:109 Ridgemont 
Crescent,city:Fernie,age:55}
while $srcObj was only:   {name:Tracine,address:109 Ridgemont 
Crescent,city:Fernie}
So $dstObj is not made EQUAL to $srcObj
in this case,
$resultObj is     
{result:true,new:[],update:[{name:Tracine,old:Chris},{address:109 Ridgemont 
Crescent,old:5619 Vanlerberg Road}]}

—————————————

My full test method, which you should copy/paste and then TRACE as you execute 
it, is:
  // zzTEST_OB_UPDATE -- test OB_UPDATE( ) function
C_OBJECT($srcObj;$dstObj;$result)

$srcObj:=New object("name";"Chris";"address";"5619 Vanlerberg 
Road";"city";"Fernie";"age";55)
$dstObj:=New object("name";"Tracine";"address";"109 Ridgemont 
Crescent";"city";"Fernie")

$result:=OB_UPDATE ($dstObj;$srcObj)

  // now when you update dstObj from srcObj, and dstObj has attributes that are 
NOT in srcObj, those attributes are not touched.
// so you can update $dstObj with just a subset of attributes using $srcObj
$dstObj:=New object("name";"Chris";"address";"5619 Vanlerberg 
Road";"city";"Fernie";"age";55)
$srcObj:=New object("name";"Tracine";"address";"109 Ridgemont 
Crescent";"city";"Fernie")

$result:=OB_UPDATE ($dstObj;$srcObj)


— Chris Belanger

**********************************************************************
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**********************************************************************

Reply via email to