This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CupByteCountTransformer.j
113 lines (90 loc) · 3.19 KB
/
CupByteCountTransformer.j
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
/*
* CupByteCountTransformer.j
* Cup
*
* Created by Aparajita Fishman on March 25, 2013.
* Copyright 2013, Filmworkers Club. All rights reserved.
*/
@import <Foundation/CPByteCountFormatter.j>
@import <Foundation/CPValueTransformer.j>
var CupByteCountTransformerSharedFormatter = nil;
/*!
This class is a CPValueTransformer that converts numbers into
formatted byte counts by using a CPByteCountFormatter. For information
on how byte counts are formatted, see:
https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSByteCountFormatter_Class/Reference/Reference.html
By default this class uses a shared formatter with the following properties:
adaptive: YES
allowedUnits: CPByteCountFormatterUseKB | CPByteCountFormatterUseMB | CPByteCountFormatterUseGB
allowsNonNumericFormatting: NO
zeroPadsFractionDigits: YES
If you want to change the properties of the shared formatter, you can retrieve it with the
+sharedFormatter method.
If you want to change the properties of the formatter for a specific transformer instance,
you can either create a new CPByteCountFormatter yourself and use the -setFormatter: method,
or use the -formatter method, which will return a new instance of CPByteCountFormatter that
belongs to the transformer instance. You can then set the properties of the returned formatter
as you wish.
*/
@implementation CupByteCountTransformer : CPValueTransformer
{
CPByteCountFormatter valueFormatter;
}
/*! @ignore */
+ (void)initialize
{
if (self !== CupByteCountTransformer)
return;
[CPValueTransformer setValueTransformer:[self new]
forName:@"CupByteCountTransformer"];
CupByteCountTransformerSharedFormatter = [self makeFormatter];
}
/*! @ignore */
+ (CPByteCountFormatter)makeFormatter
{
var formatter = [CPByteCountFormatter new];
[formatter setAdaptive:YES];
[formatter setAllowedUnits:CPByteCountFormatterUseKB | CPByteCountFormatterUseMB | CPByteCountFormatterUseGB];
[formatter setAllowsNonnumericFormatting:NO];
[formatter setZeroPadsFractionDigits:YES];
return formatter;
}
/*!
Returns the shared CPByteCountFormatter used by default by all instances of this class.
*/
+ (CPByteCountFormatter)sharedFormatter
{
return CupByteCountTransformerSharedFormatter;
}
+ (Class)transformedValueClass
{
return [CPString class];
}
+ (BOOL)allowsReverseTransformation
{
return NO;
}
/*!
Returns the receiver's instance of CPByteCountFormatter. If the receiver does not yet
have its own formatter, a new CPByteCountFormatter is assigned and then returned.
*/
- (CPByteCountFormatter)formatter
{
if (!valueFormatter)
valueFormatter = [CPByteCountFormatter new];
return valueFormatter;
}
/*!
Sets the formatter used by the receiver. If aFormatter is nil, the shared formatter will be used
when transforming values.
*/
- (void)setFormatter:(CPByteCountFormatter)aFormatter
{
valueFormatter = aFormatter;
}
- (id)transformedValue:(id)value
{
value = value === nil ? 0 : value;
return [(valueFormatter ? valueFormatter : CupByteCountTransformerSharedFormatter) stringFromByteCount:value];
}
@end