Browse Source

modify:do:, reverseTrapAt:put:

Herbert Vojčík 12 years ago
parent
commit
da03bf2359
6 changed files with 188 additions and 1 deletions
  1. 50 0
      js/Trapped-Common.deploy.js
  2. 71 1
      js/Trapped-Common.js
  3. 19 0
      js/Trapped-Demo.deploy.js
  4. 24 0
      js/Trapped-Demo.js
  5. 17 0
      st/Trapped-Common.st
  6. 7 0
      st/Trapped-Demo.st

+ 50 - 0
js/Trapped-Common.deploy.js

@@ -175,6 +175,17 @@ return nil;
 }),
 smalltalk.Object);
 
+smalltalk.addMethod(
+"_reverseTrapAt_put_",
+smalltalk.method({
+selector: "reverseTrapAt:put:",
+fn: function (anObject,value){
+var self=this;
+smalltalk.send(self,"_error_",[smalltalk.send(smalltalk.send("Trapped cannot put at ","__comma",[smalltalk.send(smalltalk.send(self,"_class",[]),"_name",[])]),"__comma",[" type key."])]);
+return self}
+}),
+smalltalk.Object);
+
 smalltalk.addMethod(
 "_asTrapPathOn_",
 smalltalk.method({
@@ -209,6 +220,19 @@ return $1;
 }),
 smalltalk.String);
 
+smalltalk.addMethod(
+"_reverseTrapAt_put_",
+smalltalk.method({
+selector: "reverseTrapAt:put:",
+fn: function (anObject,value){
+var self=this;
+var $1;
+$1=smalltalk.send(anObject,"_at_put_",[self,value]);
+return $1;
+}
+}),
+smalltalk.String);
+
 smalltalk.addMethod(
 "_reverseTrapAt_",
 smalltalk.method({
@@ -230,6 +254,19 @@ catch(e) {if(e===$early)return e[0]; throw e}
 }),
 smalltalk.Symbol);
 
+smalltalk.addMethod(
+"_reverseTrapAt_put_",
+smalltalk.method({
+selector: "reverseTrapAt:put:",
+fn: function (anObject,value){
+var self=this;
+var $1;
+$1=smalltalk.send(anObject,"_perform_withArguments_",[smalltalk.send(smalltalk.send(self,"__comma",[":"]),"_asSymbol",[]),[value]]);
+return $1;
+}
+}),
+smalltalk.Symbol);
+
 smalltalk.addMethod(
 "_reverseTrapAt_",
 smalltalk.method({
@@ -245,3 +282,16 @@ return $1;
 }),
 smalltalk.Number);
 
+smalltalk.addMethod(
+"_reverseTrapAt_put_",
+smalltalk.method({
+selector: "reverseTrapAt:put:",
+fn: function (anObject,value){
+var self=this;
+var $1;
+$1=smalltalk.send(anObject,"_at_put_",[self,value]);
+return $1;
+}
+}),
+smalltalk.Number);
+

+ 71 - 1
js/Trapped-Common.js

@@ -83,7 +83,7 @@ smalltalk.TrappedDispatcher);
 
 
 smalltalk.addClass('TrappedModelWrapper', smalltalk.Object, ['dispatcher', 'payload'], 'Trapped-Common');
-smalltalk.TrappedModelWrapper.comment="I am base class for model wrappers.\x0aI wrap a model which can be any object.\x0a\x0aMy subclasses need to provide implementation for:\x0a\x09read:do:\x0a\x09(optionally) name\x0a\x0aand must initialize:\x0a\x09payload\x0a\x09dispatcher\x0a"
+smalltalk.TrappedModelWrapper.comment="I am base class for model wrappers.\x0aI wrap a model which can be any object.\x0a\x0aMy subclasses need to provide implementation for:\x0a\x09read:do:\x0a    modify:do:\x0a\x09(optionally) name\x0a\x0aand must initialize:\x0a\x09payload\x0a\x09dispatcher\x0a"
 smalltalk.addMethod(
 "_dispatcher",
 smalltalk.method({
@@ -237,6 +237,22 @@ referencedClasses: []
 }),
 smalltalk.Object);
 
+smalltalk.addMethod(
+"_reverseTrapAt_put_",
+smalltalk.method({
+selector: "reverseTrapAt:put:",
+category: '*Trapped-Common',
+fn: function (anObject,value){
+var self=this;
+smalltalk.send(self,"_error_",[smalltalk.send(smalltalk.send("Trapped cannot put at ","__comma",[smalltalk.send(smalltalk.send(self,"_class",[]),"_name",[])]),"__comma",[" type key."])]);
+return self},
+args: ["anObject", "value"],
+source: "reverseTrapAt: anObject put: value\x0a\x09self error: 'Trapped cannot put at ', self class name, ' type key.'",
+messageSends: ["error:", ",", "name", "class"],
+referencedClasses: []
+}),
+smalltalk.Object);
+
 smalltalk.addMethod(
 "_asTrapPathOn_",
 smalltalk.method({
@@ -281,6 +297,24 @@ referencedClasses: []
 }),
 smalltalk.String);
 
+smalltalk.addMethod(
+"_reverseTrapAt_put_",
+smalltalk.method({
+selector: "reverseTrapAt:put:",
+category: '*Trapped-Common',
+fn: function (anObject,value){
+var self=this;
+var $1;
+$1=smalltalk.send(anObject,"_at_put_",[self,value]);
+return $1;
+},
+args: ["anObject", "value"],
+source: "reverseTrapAt: anObject put: value\x0a\x09^anObject at: self put: value",
+messageSends: ["at:put:"],
+referencedClasses: []
+}),
+smalltalk.String);
+
 smalltalk.addMethod(
 "_reverseTrapAt_",
 smalltalk.method({
@@ -307,6 +341,24 @@ referencedClasses: ["MessageNotUnderstood"]
 }),
 smalltalk.Symbol);
 
+smalltalk.addMethod(
+"_reverseTrapAt_put_",
+smalltalk.method({
+selector: "reverseTrapAt:put:",
+category: '*Trapped-Common',
+fn: function (anObject,value){
+var self=this;
+var $1;
+$1=smalltalk.send(anObject,"_perform_withArguments_",[smalltalk.send(smalltalk.send(self,"__comma",[":"]),"_asSymbol",[]),[value]]);
+return $1;
+},
+args: ["anObject", "value"],
+source: "reverseTrapAt: anObject put: value\x0a    ^anObject perform: (self, ':') asSymbol withArguments: { value }",
+messageSends: ["perform:withArguments:", "asSymbol", ","],
+referencedClasses: []
+}),
+smalltalk.Symbol);
+
 smalltalk.addMethod(
 "_reverseTrapAt_",
 smalltalk.method({
@@ -327,3 +379,21 @@ referencedClasses: []
 }),
 smalltalk.Number);
 
+smalltalk.addMethod(
+"_reverseTrapAt_put_",
+smalltalk.method({
+selector: "reverseTrapAt:put:",
+category: '*Trapped-Common',
+fn: function (anObject,value){
+var self=this;
+var $1;
+$1=smalltalk.send(anObject,"_at_put_",[self,value]);
+return $1;
+},
+args: ["anObject", "value"],
+source: "reverseTrapAt: anObject put: value\x0a\x09^anObject at: self put: value",
+messageSends: ["at:put:"],
+referencedClasses: []
+}),
+smalltalk.Number);
+

+ 19 - 0
js/Trapped-Demo.deploy.js

@@ -68,6 +68,25 @@ return self}
 }),
 smalltalk.TrappedPlainModel);
 
+smalltalk.addMethod(
+"_modify_do_",
+smalltalk.method({
+selector: "modify:do:",
+fn: function (path,aBlock){
+var self=this;
+var data;
+var newValue;
+data=smalltalk.send(smalltalk.send(path,"_allButLast",[]),"_asTrapPathOn_",[smalltalk.send(self,"_payload",[])]);
+newValue=smalltalk.send(aBlock,"_value_",[smalltalk.send([smalltalk.send(path,"_last",[])],"_asTrapPathOn_",[data])]);
+smalltalk.send((function(){
+return smalltalk.send(smalltalk.send(path,"_last",[]),"_reverseTrapAt_put_",[data,newValue]);
+}),"_ensure_",[(function(){
+return smalltalk.send(smalltalk.send(self,"_dispatcher",[]),"_changed_",[path]);
+})]);
+return self}
+}),
+smalltalk.TrappedPlainModel);
+
 smalltalk.addMethod(
 "_read_do_",
 smalltalk.method({

+ 24 - 0
js/Trapped-Demo.js

@@ -93,6 +93,30 @@ referencedClasses: ["TrappedDumbDispatcher"]
 }),
 smalltalk.TrappedPlainModel);
 
+smalltalk.addMethod(
+"_modify_do_",
+smalltalk.method({
+selector: "modify:do:",
+category: 'action',
+fn: function (path,aBlock){
+var self=this;
+var data;
+var newValue;
+data=smalltalk.send(smalltalk.send(path,"_allButLast",[]),"_asTrapPathOn_",[smalltalk.send(self,"_payload",[])]);
+newValue=smalltalk.send(aBlock,"_value_",[smalltalk.send([smalltalk.send(path,"_last",[])],"_asTrapPathOn_",[data])]);
+smalltalk.send((function(){
+return smalltalk.send(smalltalk.send(path,"_last",[]),"_reverseTrapAt_put_",[data,newValue]);
+}),"_ensure_",[(function(){
+return smalltalk.send(smalltalk.send(self,"_dispatcher",[]),"_changed_",[path]);
+})]);
+return self},
+args: ["path", "aBlock"],
+source: "modify: path do: aBlock\x0a\x09| data newValue |\x0a    data := path allButLast asTrapPathOn: self payload.\x0a\x09newValue := aBlock value: ({ path last } asTrapPathOn: data).\x0a    [ path last reverseTrapAt: data put: newValue ] ensure: [ self dispatcher changed: path ]",
+messageSends: ["asTrapPathOn:", "payload", "allButLast", "value:", "last", "ensure:", "changed:", "dispatcher", "reverseTrapAt:put:"],
+referencedClasses: []
+}),
+smalltalk.TrappedPlainModel);
+
 smalltalk.addMethod(
 "_read_do_",
 smalltalk.method({

+ 17 - 0
st/Trapped-Common.st

@@ -49,6 +49,7 @@ I wrap a model which can be any object.
 
 My subclasses need to provide implementation for:
 	read:do:
+    modify:do:
 	(optionally) name
 
 and must initialize:
@@ -98,6 +99,10 @@ start
 
 reverseTrapAt: anObject
 	^nil
+!
+
+reverseTrapAt: anObject put: value
+	self error: 'Trapped cannot put at ', self class name, ' type key.'
 ! !
 
 !SequenceableCollection methodsFor: '*Trapped-Common'!
@@ -112,17 +117,29 @@ asTrapPathOn: anObject
 
 reverseTrapAt: anObject
 	^anObject at: self ifAbsent: [nil]
+!
+
+reverseTrapAt: anObject put: value
+	^anObject at: self put: value
 ! !
 
 !Symbol methodsFor: '*Trapped-Common'!
 
 reverseTrapAt: anObject
 	^[anObject perform: self] on: MessageNotUnderstood do: [^nil]
+!
+
+reverseTrapAt: anObject put: value
+    ^anObject perform: (self, ':') asSymbol withArguments: { value }
 ! !
 
 !Number methodsFor: '*Trapped-Common'!
 
 reverseTrapAt: anObject
 	^anObject at: self ifAbsent: [nil]
+!
+
+reverseTrapAt: anObject put: value
+	^anObject at: self put: value
 ! !
 

+ 7 - 0
st/Trapped-Demo.st

@@ -40,6 +40,13 @@ TrappedModelWrapper subclass: #TrappedPlainModel
 
 !TrappedPlainModel methodsFor: 'action'!
 
+modify: path do: aBlock
+	| data newValue |
+    data := path allButLast asTrapPathOn: self payload.
+	newValue := aBlock value: ({ path last } asTrapPathOn: data).
+    [ path last reverseTrapAt: data put: newValue ] ensure: [ self dispatcher changed: path ]
+!
+
 read: path do: aBlock
 	| data |
     data := path asTrapPathOn: self payload.