Closed seandenigris closed 4 years ago
Totals | |
---|---|
Change from base Build 82: | 0.3% |
Covered Lines: | 8360 |
Relevant Lines: | 9004 |
Thanks for the feedback, @fortizpenaloza!
I'm intrigued about what subclasses you're creating. We're have been using it heavily for years now and, I don't remember creating any single subclass. For cents i.e we'll have a base unit and cent derived from it.
I was creating a money package. I wanted to flesh it out a bit before release. Maybe I'm using it wrong (i.e. in a non-standard or inefficient way)? I have e.g.:
BaseUnit subclass: #AmDollar
instanceVariableNames: ''
classVariableNames: ''
package: 'Aconcagua-Money'!
!AmDollar methodsFor: 'printing' stamp: 'SeanDeNigris 11/22/2019 22:18'!
printMeasure: aMeasure on: aStream
aStream nextPut: $$.
aStream nextPutAll: (aMeasure amount asFloat printPaddedWith: $0 to: 1.2).
aStream space.
aStream nextPutAll: self sign! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
AmDollar class
instanceVariableNames: 'uniqueInstance'!
!AmDollar class methodsFor: 'instance creation' stamp: 'SeanDeNigris 4/1/2015 21:36'!
new
^ self uniqueInstance! !
!AmDollar class methodsFor: 'accessing' stamp: 'SeanDeNigris 9/9/2020 15:05'!
parser
^ RxMatcher forString: '\$(\d+(.\d+)?)'.
"| symbol amount |
symbol := $$ asPParser.
amount := #digit asPParser plus, ($. asPParser, #digit asPParser, #digit asPParser) optional.
^ (symbol, amount flatten) ==> [ :n | n second asNumber dollars ]"! !
!AmDollar class methodsFor: 'accessing' stamp: 'SeanDeNigris 4/1/2015 21:38'!
uniqueInstance
uniqueInstance ifNotNil: [ ^ uniqueInstance ].
^ uniqueInstance := super new initializeNameFomOne: 'dollar' nameForMany: 'dollars' sign: 'USD'! !
!AmDollar class methodsFor: 'accessing' stamp: 'SeanDeNigris 9/9/2020 15:12'!
fromString: aString
| parser amount |
parser := self parser.
(parser matches: aString) ifFalse: [ self error ].
amount := (parser subexpression: 2) asNumber.
^ amount dollars.! !
Did you evaluate adding this message to
UnitBehavior
?
No. SimpleUnit
covered the two cases I needed. Would that be a better place?
Thanks for the feedback, @fortizpenaloza!
I'm intrigued about what subclasses you're creating. We're have been using it heavily for years now and, I don't remember creating any single subclass. For cents i.e we'll have a base unit and cent derived from it.
I was creating a money package. I wanted to flesh it out a bit before release. Maybe I'm using it wrong (i.e. in a non-standard or inefficient way)? I have e.g.:
BaseUnit subclass: #AmDollar instanceVariableNames: '' classVariableNames: '' package: 'Aconcagua-Money'! !AmDollar methodsFor: 'printing' stamp: 'SeanDeNigris 11/22/2019 22:18'! printMeasure: aMeasure on: aStream aStream nextPut: $$. aStream nextPutAll: (aMeasure amount asFloat printPaddedWith: $0 to: 1.2). aStream space. aStream nextPutAll: self sign! ! "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "! AmDollar class instanceVariableNames: 'uniqueInstance'! !AmDollar class methodsFor: 'instance creation' stamp: 'SeanDeNigris 4/1/2015 21:36'! new ^ self uniqueInstance! ! !AmDollar class methodsFor: 'accessing' stamp: 'SeanDeNigris 9/9/2020 15:05'! parser ^ RxMatcher forString: '\$(\d+(.\d+)?)'. "| symbol amount | symbol := $$ asPParser. amount := #digit asPParser plus, ($. asPParser, #digit asPParser, #digit asPParser) optional. ^ (symbol, amount flatten) ==> [ :n | n second asNumber dollars ]"! ! !AmDollar class methodsFor: 'accessing' stamp: 'SeanDeNigris 4/1/2015 21:38'! uniqueInstance uniqueInstance ifNotNil: [ ^ uniqueInstance ]. ^ uniqueInstance := super new initializeNameFomOne: 'dollar' nameForMany: 'dollars' sign: 'USD'! ! !AmDollar class methodsFor: 'accessing' stamp: 'SeanDeNigris 9/9/2020 15:12'! fromString: aString | parser amount | parser := self parser. (parser matches: aString) ifFalse: [ self error ]. amount := (parser subexpression: 2) asNumber. ^ amount dollars.! !
No, it is ok. We use formatters to print measures. I guess it didn't occur to me that it could be done this other way because I'm so used to working with the library the other way.
Did you evaluate adding this message to
UnitBehavior
?No.
SimpleUnit
covered the two cases I needed. Would that be a better place?
Can you open an issue as @gcotelli proposed?
We use formatters to print measures. I guess it didn't occur to me that it could be done this other way because I'm so used to working with the library the other way.
I'm intrigued by this, @fortizpenaloza. Is there any publicly available code I can look at to understand this approach better?
open an issue to better think if we should move something to UnitBehavior Done. #22
No, just this one https://github.com/ba-st/Buoy/blob/6d01b2e41caba920e1c4edd2fe4933b219234e5b/source/Buoy-Collections/CollectionFormatter.class.st
The simplest way to explain it is that printing is presentation logic. So, the #printOn: is just used in inspectors.
Printing these objects into web views or reports requires, at least for us, many ways to represent them. And since we avoid mix presentation logic into our model, we encapsulate these into formatters.
For example, a price formatter collaborates with a locale (usually the user') to decide where to put the sign. If after or before. And, you can instantiate it configured to show as many decimals as you want.
The context of these decisions is a huge financial application were classes are count by thousands. I'm telling you this because your approach maybe is good enough for you 😊