Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Custom Printing Broke Regular; Should Be Avail. to All Units #21

Merged

Conversation

seandenigris
Copy link

  • All tests now pass. Neglected to run all tests before submitting the custom printing hook, so new test passed, but existing ones failed.
  • While we're at it, push up the hook into SimpleUnit so that DerivedUnits can take advantage of it; add a test for this

- All tests now pass. Neglected to run all tests before submitting the custom printing hook, so new test passed, but existing ones failed.
- While we're at it, push up the hook into SimpleUnit so that DerivedUnits can take advantage of it; add a test for this
@coveralls
Copy link

coveralls commented Sep 9, 2020

Pull Request Test Coverage Report for Build 83

  • 21 of 21 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 92.848%

Totals Coverage Status
Change from base Build 82: 0.3%
Covered Lines: 8360
Relevant Lines: 9004

💛 - Coveralls

Copy link
Member

@fortizpenaloza fortizpenaloza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @seandenigris , thanks!

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.

dollar := BaseUnit named: 'dolar' sign: '$'.
cent := ProportionalDerivedUnit baseUnit: dollar conversionFactor: 1 / 100 named: 'cent'.

I'm just curious.

Did you evaluate adding this message to UnitBehavior?

@seandenigris
Copy link
Author

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?

Copy link
Member

@gcotelli gcotelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with merging this changes now to fix the tests and open an issue to better think if we should move something to UnitBehavior

@gcotelli gcotelli merged commit 77ac2ab into ba-st:release-candidate Sep 10, 2020
@fortizpenaloza
Copy link
Member

fortizpenaloza commented Sep 10, 2020

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?

@seandenigris seandenigris deleted the bug_non-custom-printing branch September 10, 2020 16:35
@seandenigris
Copy link
Author

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?

@seandenigris
Copy link
Author

open an issue to better think if we should move something to UnitBehavior
Done. #22

@fortizpenaloza
Copy link
Member

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 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants