public
Created

Fileout of Answer to StackOverflow question 11220024 How do I get a message sender in SmallTalk (Pharo)?

  • Download Gist
gistfile1.st
Smalltalk
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
Error subclass: #CartExpiredError
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Cart'!
 
!CartExpiredError methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:30'!
initialize
 
super initialize.
self messageText: '1|', UserInterface cartHasExpiredErrorMessage.! !
 
Error subclass: #InvalidCartError
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Cart'!
 
!InvalidCartError methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:06'!
initialize
 
self messageText: '1|', UserInterface invalidCartIdErrorMessage.! !
 
Object subclass: #Cart
instanceVariableNames: 'fail'
classVariableNames: ''
poolDictionaries: ''
category: 'Cart'!
 
!Cart methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:11'!
add: aQuantity booksWithISBN: aBookISBN
 
fail ifTrue: [ AddBookError signal ].! !
 
!Cart methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 11:39'!
fail
 
fail := true.! !
 
!Cart methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 11:44'!
initialize
 
fail := false.! !
 
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
 
Cart class
instanceVariableNames: ''!
 
!Cart class methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 11:39'!
failing
 
^ self new fail.! !
 
Error subclass: #AddBookError
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Cart'!
 
!AddBookError methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:50'!
initialize
 
self messageText: '1|failAdd'.! !
 
Object subclass: #Site
instanceVariableNames: 'carts cartCreationDateAndTime'
classVariableNames: ''
poolDictionaries: ''
category: 'Cart'!
 
!Site methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:40'!
add: aQuantity booksWithISBN: aBookISBN toCart: aCartId
 
| aCart |
[aCart := self retrieveCart: aCartId.
aCart add: aQuantity booksWithISBN: aBookISBN]
on: Error
do: [ :e | ^ e messageText ].
^ '0|OK'.! !
 
!Site methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 11:45'!
addCart: cart
 
carts at: cart put: cart! !
 
!Site methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 11:33'!
cart: aSmallInteger wasCreatedAt: aTime
 
cartCreationDateAndTime at: aSmallInteger put: aTime.! !
 
!Site methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:08'!
cartNumber: aCartId
 
^ carts at: aCartId ifAbsent: [ InvalidCartError signal ].! !
 
!Site methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 11:19'!
client
 
self add: 1 booksWithISBN: 2 toCart: 3.! !
 
!Site methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 11:35'!
initialize
 
cartCreationDateAndTime := Dictionary new.
carts := Dictionary new.! !
 
!Site methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:24'!
oldAdd: aQuantity booksWithISBN: aBookISBN toCart: aCartId
 
| aCart aContext |
aCart := self retrieveCart: aCartId.
 
[aCart add: aQuantity booksWithISBN: aBookISBN]
on: Error
do: [ :anError | ^'1|', (self formatAsResponse: anError messageText) ].
 
^'0|OK'.! !
 
!Site methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:36'!
retrieveCart: aCartId
 
| aCartCreationDateAndTime |
aCartCreationDateAndTime := self timestampFor: aCartId.
Time now > (aCartCreationDateAndTime + 30 minutes) ifTrue: [ CartExpiredError signal ].
^ self cartNumber: aCartId.! !
 
!Site methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:24'!
retrieveCart: aCartId context: aContext
 
| aCartCreationDateAndTime aCart |
 
[aCartCreationDateAndTime := cartCreationDateAndTime at: aCartId asInteger.]
on: Error
do: [ aContext return: ('1|', UserInterface invalidCartIdErrorMessage).].
 
(Time now > (aCartCreationDateAndTime + 30 minutes))
ifTrue: [aContext return: ('1|', ('UserInterface cartHasExpiredErrorMessage'))].
 
[aCart := carts at: aCartId asInteger.]
on: KeyNotFound
do: [ aContext return: ('1|', ('UserInterface invalidCartIdErrorMessage'))].
 
^aCart.! !
 
!Site methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:06'!
timestampFor: aCart
 
^ cartCreationDateAndTime at: aCart ifAbsent: [ InvalidCartError signal ].! !
 
TestCase subclass: #CartTest
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Cart'!
 
!CartTest methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:50'!
testAddError
 
| result site cart |
site := Site new.
cart := Cart failing.
site cart: cart wasCreatedAt: (DateAndTime now - 1 minute).
site addCart: cart.
result := site add: 1 booksWithISBN: 2 toCart: cart.
self assert: result equals: '1|failAdd'.! !
 
!CartTest methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:50'!
testExpiredCart
 
| result site |
site := Site new.
site cart: 3 wasCreatedAt: (DateAndTime now - 1 day).
result := site add: 1 booksWithISBN: 2 toCart: 3.
self assert: result equals: '1|expiredCart'.! !
 
!CartTest methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:49'!
testInvalidCart
 
| result site |
site := Site new.
site cart: 3 wasCreatedAt: (DateAndTime now - 1 minute).
result := site add: 1 booksWithISBN: 2 toCart: 3.
self assert: result equals: '1|invalidCart'.! !
 
!CartTest methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:49'!
testInvalidTime
 
| result |
result := Site new add: 1 booksWithISBN: 2 toCart: 3.
self assert: result equals: '1|invalidCart'.! !
 
!CartTest methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 11:43'!
testSuccess
 
| result site cart |
site := Site new.
cart := Cart new.
site cart: cart wasCreatedAt: (DateAndTime now - 1 minute).
site addCart: cart.
result := site add: 1 booksWithISBN: 2 toCart: cart.
self assert: result equals: '0|OK'.! !
 
Object subclass: #UserInterface
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Cart'!
 
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
 
UserInterface class
instanceVariableNames: ''!
 
!UserInterface class methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:49'!
cartHasExpiredErrorMessage
 
^ 'expiredCart'! !
 
!UserInterface class methodsFor: 'as yet unclassified' stamp: 'SeanDeNigris 6/27/2012 12:49'!
invalidCartIdErrorMessage
 
^ 'invalidCart'! !

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.