Trapped-Backend.st 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. Smalltalk current createPackage: 'Trapped-Backend' properties: #{}!
  2. Object subclass: #EavModel
  3. instanceVariableNames: 'getBlock putBlock'
  4. package: 'Trapped-Backend'!
  5. !EavModel commentStamp!
  6. External actor value model.!
  7. !EavModel methodsFor: 'accessing'!
  8. getBlock: aBlock
  9. getBlock := aBlock
  10. !
  11. on: anObject
  12. "Returns value of model applied on object"
  13. ^getBlock value: anObject
  14. !
  15. on: anObject put: anObject2
  16. "Puts a value via model applied on object"
  17. ^putBlock value: anObject value: anObject2
  18. !
  19. putBlock: aBlock
  20. putBlock := aBlock
  21. ! !
  22. !EavModel methodsFor: 'initialization'!
  23. initialize
  24. super initialize.
  25. getBlock := [ self error: 'No getter block.' ].
  26. putBlock := [ self error: 'No putter block.' ].
  27. ! !
  28. Object subclass: #Isolator
  29. instanceVariableNames: 'root'
  30. package: 'Trapped-Backend'!
  31. !Isolator methodsFor: 'accessing'!
  32. root
  33. ^root
  34. !
  35. root: anObject
  36. root := anObject
  37. ! !
  38. !Isolator methodsFor: 'action'!
  39. model: anEavModel modify: aBlock
  40. | newValue |
  41. newValue := aBlock value: (anEavModel on: self).
  42. anEavModel on: self put: newValue deepCopy
  43. !
  44. model: anEavModel read: aBlock
  45. aBlock value: (anEavModel on: self) deepCopy
  46. ! !
  47. !Isolator class methodsFor: 'instance creation'!
  48. on: anObject
  49. ^self new root: anObject
  50. ! !
  51. !Object methodsFor: '*Trapped-Backend'!
  52. reverseTrapAt: anObject
  53. ^nil
  54. !
  55. reverseTrapAt: anObject put: value
  56. self error: 'Trapped cannot put at ', self class name, ' type key.'
  57. ! !
  58. !SequenceableCollection methodsFor: '*Trapped-Backend'!
  59. asEavModel
  60. | model |
  61. model := EavModel new.
  62. model getBlock: [ :anObject |
  63. self inject: anObject into: [ :soFar :segment |
  64. soFar ifNotNil: [ segment reverseTrapAt: soFar ]]].
  65. self isEmpty ifFalse: [
  66. model putBlock: [ :anObject :value | | penultimate |
  67. penultimate := self allButLast inject: anObject into: [ :soFar :segment |
  68. soFar ifNotNil: [ segment reverseTrapAt: soFar ]].
  69. self last reverseTrapAt: penultimate put: value ]].
  70. ^model
  71. ! !
  72. !String methodsFor: '*Trapped-Backend'!
  73. reverseTrapAt: anObject
  74. ^anObject at: self ifAbsent: [nil]
  75. !
  76. reverseTrapAt: anObject put: value
  77. ^anObject at: self put: value
  78. ! !
  79. !Symbol methodsFor: '*Trapped-Backend'!
  80. reverseTrapAt: anObject
  81. ^[anObject perform: self] on: MessageNotUnderstood do: [^nil]
  82. !
  83. reverseTrapAt: anObject put: value
  84. ^anObject perform: (self, ':') asSymbol withArguments: { value }
  85. ! !
  86. !Number methodsFor: '*Trapped-Backend'!
  87. reverseTrapAt: anObject
  88. ^anObject at: self ifAbsent: [nil]
  89. !
  90. reverseTrapAt: anObject put: value
  91. ^anObject at: self put: value
  92. ! !