31
31
32
32
@interface InCodeMappingProvider ()
33
33
@property (nonatomic , strong ) NSMutableDictionary *mappingDictionary;
34
+ @property (nonatomic , strong ) NSMutableDictionary *inverseMappingDictionary;
34
35
@property (nonatomic , strong ) NSMutableDictionary *dateFormatterDictionary;
36
+ @property (nonatomic , strong ) NSMutableDictionary *inverseDateFormatterDictionary;
35
37
@end
36
38
37
39
@implementation InCodeMappingProvider
@@ -44,8 +46,11 @@ - (id)init
44
46
{
45
47
if (self = [super init ])
46
48
{
49
+ self.automaticallyGenerateInverseMapping = YES ;
47
50
self.mappingDictionary = [NSMutableDictionary dictionary ];
51
+ self.inverseMappingDictionary = [NSMutableDictionary dictionary ];
48
52
self.dateFormatterDictionary = [NSMutableDictionary dictionary ];
53
+ self.inverseDateFormatterDictionary = [NSMutableDictionary dictionary ];
49
54
}
50
55
51
56
return self;
@@ -58,6 +63,11 @@ - (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *
58
63
ObjectMappingInfo *info = [[ObjectMappingInfo alloc ] initWithDictionaryKey: dictionaryKey propertyKey: propertyKey andObjectType: objectType];
59
64
NSString *key = [self uniqueKeyForClass: class andKey: dictionaryKey];
60
65
[self .mappingDictionary setObject: info forKey: key];
66
+
67
+ if (self.automaticallyGenerateInverseMapping )
68
+ {
69
+ [self mapFromPropertyKey: propertyKey toDictionaryKey: dictionaryKey forClass: class];
70
+ }
61
71
}
62
72
63
73
- (void )mapFromDictionaryKey : (NSString *)dictionaryKey toPropertyKey : (NSString *)propertyKey forClass : (Class )class
@@ -72,10 +82,33 @@ - (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *
72
82
[self .mappingDictionary setObject: info forKey: key];
73
83
}
74
84
75
- - (void )setDateFormatter : (NSDateFormatter *)dateFormatter forProperty : (NSString *)property andClass : (Class )class
85
+ - (void )mapFromPropertyKey : (NSString *)propertyKey toDictionaryKey : (NSString *)dictionaryKey forClass : (Class )class {
86
+ ObjectMappingInfo *info = [[ObjectMappingInfo alloc ] initWithDictionaryKey: dictionaryKey propertyKey: propertyKey andObjectType: nil ];
87
+ NSString *key = [self uniqueKeyForClass: class andKey: propertyKey];
88
+ [self .inverseMappingDictionary setObject: info forKey: key];
89
+ }
90
+
91
+ - (void )mapFromPropertyKey : (NSString *)propertyKey toDictionaryKey : (NSString *)dictionaryKey forClass : (Class )class withTransformer : (MappingTransformer)transformer {
92
+ ObjectMappingInfo *info = [[ObjectMappingInfo alloc ] initWithDictionaryKey: dictionaryKey propertyKey: propertyKey andTransformer: transformer];
93
+ NSString *key = [self uniqueKeyForClass: class andKey: propertyKey];
94
+ [self .inverseMappingDictionary setObject: info forKey: key];
95
+ }
96
+
97
+ - (void )setDateFormatter : (NSDateFormatter *)dateFormatter forPropertyKey : (NSString *)property andClass : (Class )class
76
98
{
77
99
NSString *key = [self uniqueKeyForClass: class andKey: property];
78
100
[self .dateFormatterDictionary setObject: dateFormatter forKey: key];
101
+
102
+ if (self.automaticallyGenerateInverseMapping )
103
+ {
104
+ [self setDateFormatter: dateFormatter forDictionaryKey: property andClass: class];
105
+ }
106
+ }
107
+
108
+ - (void )setDateFormatter : (NSDateFormatter *)dateFormatter forDictionaryKey : (NSString *)dictionaryKey andClass : (Class )class
109
+ {
110
+ NSString *key = [self uniqueKeyForClass: class andKey: dictionaryKey];
111
+ [self .inverseDateFormatterDictionary setObject: dateFormatter forKey: key];
79
112
}
80
113
81
114
#pragma mark - public Methods -
@@ -93,9 +126,20 @@ - (ObjectMappingInfo *)mappingInfoForClass:(Class)class andDictionaryKey:(NSStri
93
126
return [self .mappingDictionary objectForKey: key];
94
127
}
95
128
96
- - (NSDateFormatter *)dateFormatterForClass : (Class )class andProperty : (NSString *)property
129
+ - (ObjectMappingInfo *)mappingInfoForClass : (Class )class andPropertyKey : (NSString *)source {
130
+ NSString *key = [self uniqueKeyForClass: class andKey: source];
131
+ return [self .inverseMappingDictionary objectForKey: key];
132
+ }
133
+
134
+ - (NSDateFormatter *)dateFormatterForClass : (Class )class andPropertyKey : (NSString *)propertyKey
97
135
{
98
- NSString *key = [self uniqueKeyForClass: class andKey: property];
136
+ NSString *key = [self uniqueKeyForClass: class andKey: propertyKey];
137
+ return [self .dateFormatterDictionary objectForKey: key];
138
+ }
139
+
140
+ - (NSDateFormatter *)dateFormatterForClass : (Class )class andDictionaryKey : (NSString *)dictionaryKey
141
+ {
142
+ NSString *key = [self uniqueKeyForClass: class andKey: dictionaryKey];
99
143
return [self .dateFormatterDictionary objectForKey: key];
100
144
}
101
145
0 commit comments