-
Notifications
You must be signed in to change notification settings - Fork 20
/
MBValueTransformer.m
executable file
·81 lines (56 loc) · 1.97 KB
/
MBValueTransformer.m
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
//
// MBValueTransformer.m
//
#import "MBValueTransformer.h"
//
// Any BGMValueTransformer supporting reverse transformation. Necessary because
// +allowsReverseTransformation is a class method.
//
@interface MTLReversibleValueTransformer : MBValueTransformer
@end
@interface MBValueTransformer ()
@end
@implementation MBValueTransformer
#pragma mark Lifecycle
+ (instancetype)transformerWithBlock:(BGMValueTransformerBlock)transformationBlock {
return [[self alloc] initWithForwardBlock:transformationBlock reverseBlock:nil];
}
+ (instancetype)reversibleTransformerWithBlock:(BGMValueTransformerBlock)transformationBlock {
return [self reversibleTransformerWithForwardBlock:transformationBlock reverseBlock:transformationBlock];
}
+ (instancetype)reversibleTransformerWithForwardBlock:(BGMValueTransformerBlock)forwardBlock reverseBlock:(BGMValueTransformerBlock)reverseBlock {
return [[MTLReversibleValueTransformer alloc] initWithForwardBlock:forwardBlock reverseBlock:reverseBlock];
}
- (id)initWithForwardBlock:(BGMValueTransformerBlock)forwardBlock reverseBlock:(BGMValueTransformerBlock)reverseBlock {
NSParameterAssert(forwardBlock != nil);
self = [super init];
if (self == nil) return nil;
_forwardBlock = [forwardBlock copy];
_reverseBlock = [reverseBlock copy];
return self;
}
#pragma mark NSValueTransformer
+ (BOOL)allowsReverseTransformation {
return NO;
}
+ (Class)transformedValueClass {
return [NSObject class];
}
- (id)transformedValue:(id)value {
return self.forwardBlock(value);
}
@end
@implementation MTLReversibleValueTransformer
#pragma mark Lifecycle
- (id)initWithForwardBlock:(BGMValueTransformerBlock)forwardBlock reverseBlock:(BGMValueTransformerBlock)reverseBlock {
NSParameterAssert(reverseBlock != nil);
return [super initWithForwardBlock:forwardBlock reverseBlock:reverseBlock];
}
#pragma mark NSValueTransformer
+ (BOOL)allowsReverseTransformation {
return YES;
}
- (id)reverseTransformedValue:(id)value {
return self.reverseBlock(value);
}
@end