Skip to content

@seandenigris /gist:3005341
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fileout of Answer to StackOverflow question 11220024 How do I get a message sender in SmallTalk (Pharo)?
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'! !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.