forked from SeasideSt/Grease
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGRUnitPrinter.class.st
More file actions
72 lines (64 loc) · 1.58 KB
/
GRUnitPrinter.class.st
File metadata and controls
72 lines (64 loc) · 1.58 KB
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
Class {
#name : 'GRUnitPrinter',
#superclass : 'GRPrinter',
#instVars : [
'integerPrinter',
'fractionPrinter',
'units',
'base'
],
#category : 'Grease-Core-Text',
#package : 'Grease-Core',
#tag : 'Text'
}
{ #category : 'instance creation' }
GRUnitPrinter class >> base: anInteger units: anArray [
^ self new
base: anInteger;
units: anArray;
yourself
]
{ #category : 'accessing' }
GRUnitPrinter >> base: anInteger [
base := anInteger
]
{ #category : 'accessing' }
GRUnitPrinter >> fractionPrinter: aPrinter [
fractionPrinter := aPrinter
]
{ #category : 'initialization' }
GRUnitPrinter >> initialize [
super initialize.
self integerPrinter: (GRNumberPrinter new
precision: 0;
yourself).
self fractionPrinter: (GRNumberPrinter new
precision: 1;
yourself)
]
{ #category : 'accessing' }
GRUnitPrinter >> integerPrinter: aPrinter [
integerPrinter := aPrinter
]
{ #category : 'printing' }
GRUnitPrinter >> print: anObject on: aStream [
anObject = 1
ifTrue: [ ^ self print: anObject unit: units first on: aStream ].
units allButFirst
inject: anObject asFloat
into: [ :value :each |
value < base
ifFalse: [ value / base ]
ifTrue: [ ^ self print: value unit: each on: aStream ] ]
]
{ #category : 'printing' }
GRUnitPrinter >> print: aNumber unit: aString on: aStream [
(units first = aString or: [ units second = aString ])
ifTrue: [ integerPrinter print: aNumber on: aStream ]
ifFalse: [ fractionPrinter print: aNumber on: aStream ].
aStream nextPut: $ ; nextPutAll: aString
]
{ #category : 'accessing' }
GRUnitPrinter >> units: anArray [
units := anArray
]