Skip to content

Commit a3cbf8b

Browse files
committed
Merge pull request #12 from aryaxt/AddInverseMapping
Added inverse mapping #11
2 parents f05d78f + 3a66439 commit a3cbf8b

File tree

10 files changed

+197
-199
lines changed

10 files changed

+197
-199
lines changed

OCMapper.xcodeproj/project.pbxproj

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
1502B3B117CDA75B00C095DE /* GoogleSearchResult.m in Sources */ = {isa = PBXBuildFile; fileRef = 1502B3B017CDA75B00C095DE /* GoogleSearchResult.m */; };
2828
1502B3B317CDADE300C095DE /* Storyboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1502B3B217CDADE300C095DE /* Storyboard.storyboard */; };
2929
15371EC01727879700A508F4 /* ObjectMappingConfig.plist in Resources */ = {isa = PBXBuildFile; fileRef = 15371EBF1727879700A508F4 /* ObjectMappingConfig.plist */; };
30-
15371EC517278C6300A508F4 /* PLISTMappingProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 15371EC417278C6300A508F4 /* PLISTMappingProvider.m */; };
3130
157B3076171E3655005AAB02 /* CoreDataManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 157B3075171E3655005AAB02 /* CoreDataManager.m */; };
3231
157B307C171E36D4005AAB02 /* CDUser.m in Sources */ = {isa = PBXBuildFile; fileRef = 157B307B171E36D4005AAB02 /* CDUser.m */; };
3332
157B307F171E3714005AAB02 /* OCMapper.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 157B307D171E3714005AAB02 /* OCMapper.xcdatamodeld */; };
@@ -113,8 +112,6 @@
113112
1502B3B017CDA75B00C095DE /* GoogleSearchResult.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GoogleSearchResult.m; sourceTree = "<group>"; };
114113
1502B3B217CDADE300C095DE /* Storyboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Storyboard.storyboard; sourceTree = "<group>"; };
115114
15371EBF1727879700A508F4 /* ObjectMappingConfig.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = ObjectMappingConfig.plist; sourceTree = "<group>"; };
116-
15371EC317278C6300A508F4 /* PLISTMappingProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PLISTMappingProvider.h; sourceTree = "<group>"; };
117-
15371EC417278C6300A508F4 /* PLISTMappingProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PLISTMappingProvider.m; sourceTree = "<group>"; };
118115
157B3074171E3655005AAB02 /* CoreDataManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CoreDataManager.h; path = ../CoreDataManager.h; sourceTree = "<group>"; };
119116
157B3075171E3655005AAB02 /* CoreDataManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CoreDataManager.m; path = ../CoreDataManager.m; sourceTree = "<group>"; };
120117
157B3078171E3673005AAB02 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; };
@@ -261,15 +258,6 @@
261258
path = Models;
262259
sourceTree = "<group>";
263260
};
264-
15371EC217278C4500A508F4 /* PLIST Mapping */ = {
265-
isa = PBXGroup;
266-
children = (
267-
15371EC317278C6300A508F4 /* PLISTMappingProvider.h */,
268-
15371EC417278C6300A508F4 /* PLISTMappingProvider.m */,
269-
);
270-
path = "PLIST Mapping";
271-
sourceTree = "<group>";
272-
};
273261
157B3084171E4518005AAB02 /* Core Data */ = {
274262
isa = PBXGroup;
275263
children = (
@@ -314,7 +302,6 @@
314302
157B315017237C75005AAB02 /* Mapping Provider */ = {
315303
isa = PBXGroup;
316304
children = (
317-
15371EC217278C4500A508F4 /* PLIST Mapping */,
318305
15EF052D172783F70041358D /* In Code Mapping */,
319306
157B315217237C87005AAB02 /* MappingProvider.h */,
320307
15E2B97B171BEAEB00526C77 /* ObjectMappingInfo.h */,
@@ -581,7 +568,6 @@
581568
157B314417234ACB005AAB02 /* ObjectInstanceProvider.m in Sources */,
582569
157B314717234ADF005AAB02 /* ManagedObjectInstanceProvider.m in Sources */,
583570
15EF0535172783F70041358D /* InCodeMappingProvider.m in Sources */,
584-
15371EC517278C6300A508F4 /* PLISTMappingProvider.m in Sources */,
585571
15A1933F1766CBB800016904 /* CommonLoggingProvider.m in Sources */,
586572
1502B37117CD94EA00C095DE /* SampleViewController.m in Sources */,
587573
1502B38A17CD985300C095DE /* AFHTTPClient.m in Sources */,

OCMapper/Source/Mapping Provider/In Code Mapping/InCodeMappingProvider.h

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,79 @@
3131

3232
@interface InCodeMappingProvider : NSObject <MappingProvider>
3333

34+
/**
35+
* Defaults to true, and if the value is true
36+
* If true, for every dictionary-to-property mapping, it creates an inverse mapping
37+
* So when a mapping is written to convert a dictionary key named "dob" to a property named "dateOfBirth"
38+
* an inverse mapping is generated. Next time you convert that object to a dictionary,
39+
* the dictionary key would be named "dob",which is mapped from a property named "dateOfBirth"
40+
*/
41+
@property (nonatomic, assign) BOOL automaticallyGenerateInverseMapping;
42+
43+
/**
44+
* Set key/property Mapping to be used for converting a dictionary to a model object
45+
*
46+
* @param dictionaryKey NSString key in the dictionary
47+
* @param propertyKey NSString name of the property
48+
* @param objectType Class to be instantiated and assigned to property
49+
* @param class Class to be assign mapping to
50+
*/
3451
- (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *)propertyKey withObjectType:(Class)objectType forClass:(Class)class;
52+
53+
/**
54+
* Set key/property Mapping to be used for converting a dictionary to a model object
55+
*
56+
* @param dictionaryKey NSString key in the dictionary
57+
* @param propertyKey NSString name of the property
58+
* @param class Class to be assign mapping to
59+
* @param transformer Block to be used for transforming an item in dictionary into a desired result and assign to property
60+
*/
3561
- (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *)propertyKey forClass:(Class)class withTransformer:(MappingTransformer)transformer;
62+
63+
/**
64+
* Set key/property Mapping to be used for converting a dictionary to a model object
65+
*
66+
* @param dictionaryKey NSString key in the dictionary
67+
* @param propertyKey NSString name of the property
68+
* @param class Class to be assign mapping to
69+
*/
3670
- (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *)propertyKey forClass:(Class)class;
37-
- (void)setDateFormatter:(NSDateFormatter *)dateFormatter forProperty:(NSString *)property andClass:(Class)class;
71+
72+
/**
73+
* Set key/property Mapping to be used for converting a model object to dictionary
74+
*
75+
* @param dictionaryKey NSString key in the dictionary
76+
* @param propertyKey NSString name of the property
77+
* @param class Class to be assign mapping to
78+
*/
79+
- (void)mapFromPropertyKey:(NSString *)propertyKey toDictionaryKey:(NSString *)dictionaryKey forClass:(Class)class;
80+
81+
/**
82+
* Set key/property Mapping to be used for converting a model object to dictionary
83+
*
84+
* @param dictionaryKey NSString key in the dictionary
85+
* @param propertyKey NSString name of the property
86+
* @param class Class to be assign mapping to
87+
* @param transformer Block to be used for transforming an item in dictionary into a desired result and assign to property
88+
*/
89+
- (void)mapFromPropertyKey:(NSString *)propertyKey toDictionaryKey:(NSString *)dictionaryKey forClass:(Class)class withTransformer:(MappingTransformer)transformer;
90+
91+
/**
92+
* Set dateformatter to be used for converting a dictionary to a model object
93+
*
94+
* @param dateFormatter NSDateFormatter a dateformatter
95+
* @param propertyKey NSString name of the property
96+
* @param class Class to be assign mapping to
97+
*/
98+
- (void)setDateFormatter:(NSDateFormatter *)dateFormatter forPropertyKey:(NSString *)propertyKey andClass:(Class)class;
99+
100+
/**
101+
* Set dateformatter to be used for converting a model object to a dictionary
102+
*
103+
* @param dateFormatter NSDateFormatter a dateformatter
104+
* @param propertyKey NSString name of the property
105+
* @param class Class to be assign mapping to
106+
*/
107+
- (void)setDateFormatter:(NSDateFormatter *)dateFormatter forDictionaryKey:(NSString *)dictionaryKey andClass:(Class)class;
38108

39109
@end

OCMapper/Source/Mapping Provider/In Code Mapping/InCodeMappingProvider.m

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131

3232
@interface InCodeMappingProvider()
3333
@property (nonatomic, strong) NSMutableDictionary *mappingDictionary;
34+
@property (nonatomic, strong) NSMutableDictionary *inverseMappingDictionary;
3435
@property (nonatomic, strong) NSMutableDictionary *dateFormatterDictionary;
36+
@property (nonatomic, strong) NSMutableDictionary *inverseDateFormatterDictionary;
3537
@end
3638

3739
@implementation InCodeMappingProvider
@@ -44,8 +46,11 @@ - (id)init
4446
{
4547
if (self = [super init])
4648
{
49+
self.automaticallyGenerateInverseMapping = YES;
4750
self.mappingDictionary = [NSMutableDictionary dictionary];
51+
self.inverseMappingDictionary = [NSMutableDictionary dictionary];
4852
self.dateFormatterDictionary = [NSMutableDictionary dictionary];
53+
self.inverseDateFormatterDictionary = [NSMutableDictionary dictionary];
4954
}
5055

5156
return self;
@@ -58,6 +63,11 @@ - (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *
5863
ObjectMappingInfo *info = [[ObjectMappingInfo alloc] initWithDictionaryKey:dictionaryKey propertyKey:propertyKey andObjectType:objectType];
5964
NSString *key = [self uniqueKeyForClass:class andKey:dictionaryKey];
6065
[self.mappingDictionary setObject:info forKey:key];
66+
67+
if (self.automaticallyGenerateInverseMapping)
68+
{
69+
[self mapFromPropertyKey:propertyKey toDictionaryKey:dictionaryKey forClass:class];
70+
}
6171
}
6272

6373
- (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *)propertyKey forClass:(Class)class
@@ -72,10 +82,33 @@ - (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *
7282
[self.mappingDictionary setObject:info forKey:key];
7383
}
7484

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
7698
{
7799
NSString *key = [self uniqueKeyForClass:class andKey:property];
78100
[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];
79112
}
80113

81114
#pragma mark - public Methods -
@@ -93,9 +126,20 @@ - (ObjectMappingInfo *)mappingInfoForClass:(Class)class andDictionaryKey:(NSStri
93126
return [self.mappingDictionary objectForKey:key];
94127
}
95128

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
97135
{
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];
99143
return [self.dateFormatterDictionary objectForKey:key];
100144
}
101145

OCMapper/Source/Mapping Provider/MappingProvider.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
@protocol MappingProvider <NSObject>
3232

3333
- (ObjectMappingInfo *)mappingInfoForClass:(Class)class andDictionaryKey:(NSString *)key;
34-
- (NSDateFormatter *)dateFormatterForClass:(Class)class andProperty:(NSString *)property;
34+
- (ObjectMappingInfo *)mappingInfoForClass:(Class)class andPropertyKey:(NSString *)key;
35+
- (NSDateFormatter *)dateFormatterForClass:(Class)class andPropertyKey:(NSString *)key;
36+
- (NSDateFormatter *)dateFormatterForClass:(Class)class andDictionaryKey:(NSString *)key;
3537

3638
@end

OCMapper/Source/Mapping Provider/PLIST Mapping/PLISTMappingProvider.h

Lines changed: 0 additions & 36 deletions
This file was deleted.

OCMapper/Source/Mapping Provider/PLIST Mapping/PLISTMappingProvider.m

Lines changed: 0 additions & 116 deletions
This file was deleted.

0 commit comments

Comments
 (0)