Skip to content

Commit

Permalink
Added inverse mapping (Map custom key names from object to dictionary)
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaxt committed Jan 24, 2015
1 parent fe0155b commit 3a66439
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 199 deletions.
14 changes: 0 additions & 14 deletions OCMapper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
1502B3B117CDA75B00C095DE /* GoogleSearchResult.m in Sources */ = {isa = PBXBuildFile; fileRef = 1502B3B017CDA75B00C095DE /* GoogleSearchResult.m */; };
1502B3B317CDADE300C095DE /* Storyboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1502B3B217CDADE300C095DE /* Storyboard.storyboard */; };
15371EC01727879700A508F4 /* ObjectMappingConfig.plist in Resources */ = {isa = PBXBuildFile; fileRef = 15371EBF1727879700A508F4 /* ObjectMappingConfig.plist */; };
15371EC517278C6300A508F4 /* PLISTMappingProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 15371EC417278C6300A508F4 /* PLISTMappingProvider.m */; };
157B3076171E3655005AAB02 /* CoreDataManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 157B3075171E3655005AAB02 /* CoreDataManager.m */; };
157B307C171E36D4005AAB02 /* CDUser.m in Sources */ = {isa = PBXBuildFile; fileRef = 157B307B171E36D4005AAB02 /* CDUser.m */; };
157B307F171E3714005AAB02 /* OCMapper.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 157B307D171E3714005AAB02 /* OCMapper.xcdatamodeld */; };
Expand Down Expand Up @@ -113,8 +112,6 @@
1502B3B017CDA75B00C095DE /* GoogleSearchResult.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GoogleSearchResult.m; sourceTree = "<group>"; };
1502B3B217CDADE300C095DE /* Storyboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Storyboard.storyboard; sourceTree = "<group>"; };
15371EBF1727879700A508F4 /* ObjectMappingConfig.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = ObjectMappingConfig.plist; sourceTree = "<group>"; };
15371EC317278C6300A508F4 /* PLISTMappingProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PLISTMappingProvider.h; sourceTree = "<group>"; };
15371EC417278C6300A508F4 /* PLISTMappingProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PLISTMappingProvider.m; sourceTree = "<group>"; };
157B3074171E3655005AAB02 /* CoreDataManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CoreDataManager.h; path = ../CoreDataManager.h; sourceTree = "<group>"; };
157B3075171E3655005AAB02 /* CoreDataManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CoreDataManager.m; path = ../CoreDataManager.m; sourceTree = "<group>"; };
157B3078171E3673005AAB02 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -261,15 +258,6 @@
path = Models;
sourceTree = "<group>";
};
15371EC217278C4500A508F4 /* PLIST Mapping */ = {
isa = PBXGroup;
children = (
15371EC317278C6300A508F4 /* PLISTMappingProvider.h */,
15371EC417278C6300A508F4 /* PLISTMappingProvider.m */,
);
path = "PLIST Mapping";
sourceTree = "<group>";
};
157B3084171E4518005AAB02 /* Core Data */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -314,7 +302,6 @@
157B315017237C75005AAB02 /* Mapping Provider */ = {
isa = PBXGroup;
children = (
15371EC217278C4500A508F4 /* PLIST Mapping */,
15EF052D172783F70041358D /* In Code Mapping */,
157B315217237C87005AAB02 /* MappingProvider.h */,
15E2B97B171BEAEB00526C77 /* ObjectMappingInfo.h */,
Expand Down Expand Up @@ -581,7 +568,6 @@
157B314417234ACB005AAB02 /* ObjectInstanceProvider.m in Sources */,
157B314717234ADF005AAB02 /* ManagedObjectInstanceProvider.m in Sources */,
15EF0535172783F70041358D /* InCodeMappingProvider.m in Sources */,
15371EC517278C6300A508F4 /* PLISTMappingProvider.m in Sources */,
15A1933F1766CBB800016904 /* CommonLoggingProvider.m in Sources */,
1502B37117CD94EA00C095DE /* SampleViewController.m in Sources */,
1502B38A17CD985300C095DE /* AFHTTPClient.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,79 @@

@interface InCodeMappingProvider : NSObject <MappingProvider>

/**
* Defaults to true, and if the value is true
* If true, for every dictionary-to-property mapping, it creates an inverse mapping
* So when a mapping is written to convert a dictionary key named "dob" to a property named "dateOfBirth"
* an inverse mapping is generated. Next time you convert that object to a dictionary,
* the dictionary key would be named "dob",which is mapped from a property named "dateOfBirth"
*/
@property (nonatomic, assign) BOOL automaticallyGenerateInverseMapping;

/**
* Set key/property Mapping to be used for converting a dictionary to a model object
*
* @param dictionaryKey NSString key in the dictionary
* @param propertyKey NSString name of the property
* @param objectType Class to be instantiated and assigned to property
* @param class Class to be assign mapping to
*/
- (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *)propertyKey withObjectType:(Class)objectType forClass:(Class)class;

/**
* Set key/property Mapping to be used for converting a dictionary to a model object
*
* @param dictionaryKey NSString key in the dictionary
* @param propertyKey NSString name of the property
* @param class Class to be assign mapping to
* @param transformer Block to be used for transforming an item in dictionary into a desired result and assign to property
*/
- (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *)propertyKey forClass:(Class)class withTransformer:(MappingTransformer)transformer;

/**
* Set key/property Mapping to be used for converting a dictionary to a model object
*
* @param dictionaryKey NSString key in the dictionary
* @param propertyKey NSString name of the property
* @param class Class to be assign mapping to
*/
- (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *)propertyKey forClass:(Class)class;
- (void)setDateFormatter:(NSDateFormatter *)dateFormatter forProperty:(NSString *)property andClass:(Class)class;

/**
* Set key/property Mapping to be used for converting a model object to dictionary
*
* @param dictionaryKey NSString key in the dictionary
* @param propertyKey NSString name of the property
* @param class Class to be assign mapping to
*/
- (void)mapFromPropertyKey:(NSString *)propertyKey toDictionaryKey:(NSString *)dictionaryKey forClass:(Class)class;

/**
* Set key/property Mapping to be used for converting a model object to dictionary
*
* @param dictionaryKey NSString key in the dictionary
* @param propertyKey NSString name of the property
* @param class Class to be assign mapping to
* @param transformer Block to be used for transforming an item in dictionary into a desired result and assign to property
*/
- (void)mapFromPropertyKey:(NSString *)propertyKey toDictionaryKey:(NSString *)dictionaryKey forClass:(Class)class withTransformer:(MappingTransformer)transformer;

/**
* Set dateformatter to be used for converting a dictionary to a model object
*
* @param dateFormatter NSDateFormatter a dateformatter
* @param propertyKey NSString name of the property
* @param class Class to be assign mapping to
*/
- (void)setDateFormatter:(NSDateFormatter *)dateFormatter forPropertyKey:(NSString *)propertyKey andClass:(Class)class;

/**
* Set dateformatter to be used for converting a model object to a dictionary
*
* @param dateFormatter NSDateFormatter a dateformatter
* @param propertyKey NSString name of the property
* @param class Class to be assign mapping to
*/
- (void)setDateFormatter:(NSDateFormatter *)dateFormatter forDictionaryKey:(NSString *)dictionaryKey andClass:(Class)class;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@

@interface InCodeMappingProvider()
@property (nonatomic, strong) NSMutableDictionary *mappingDictionary;
@property (nonatomic, strong) NSMutableDictionary *inverseMappingDictionary;
@property (nonatomic, strong) NSMutableDictionary *dateFormatterDictionary;
@property (nonatomic, strong) NSMutableDictionary *inverseDateFormatterDictionary;
@end

@implementation InCodeMappingProvider
Expand All @@ -44,8 +46,11 @@ - (id)init
{
if (self = [super init])
{
self.automaticallyGenerateInverseMapping = YES;
self.mappingDictionary = [NSMutableDictionary dictionary];
self.inverseMappingDictionary = [NSMutableDictionary dictionary];
self.dateFormatterDictionary = [NSMutableDictionary dictionary];
self.inverseDateFormatterDictionary = [NSMutableDictionary dictionary];
}

return self;
Expand All @@ -58,6 +63,11 @@ - (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *
ObjectMappingInfo *info = [[ObjectMappingInfo alloc] initWithDictionaryKey:dictionaryKey propertyKey:propertyKey andObjectType:objectType];
NSString *key = [self uniqueKeyForClass:class andKey:dictionaryKey];
[self.mappingDictionary setObject:info forKey:key];

if (self.automaticallyGenerateInverseMapping)
{
[self mapFromPropertyKey:propertyKey toDictionaryKey:dictionaryKey forClass:class];
}
}

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

- (void)setDateFormatter:(NSDateFormatter *)dateFormatter forProperty:(NSString *)property andClass:(Class)class
- (void)mapFromPropertyKey:(NSString *)propertyKey toDictionaryKey:(NSString *)dictionaryKey forClass:(Class)class {
ObjectMappingInfo *info = [[ObjectMappingInfo alloc] initWithDictionaryKey:dictionaryKey propertyKey:propertyKey andObjectType:nil];
NSString *key = [self uniqueKeyForClass:class andKey:propertyKey];
[self.inverseMappingDictionary setObject:info forKey:key];
}

- (void)mapFromPropertyKey:(NSString *)propertyKey toDictionaryKey:(NSString *)dictionaryKey forClass:(Class)class withTransformer:(MappingTransformer)transformer {
ObjectMappingInfo *info = [[ObjectMappingInfo alloc] initWithDictionaryKey:dictionaryKey propertyKey:propertyKey andTransformer:transformer];
NSString *key = [self uniqueKeyForClass:class andKey:propertyKey];
[self.inverseMappingDictionary setObject:info forKey:key];
}

- (void)setDateFormatter:(NSDateFormatter *)dateFormatter forPropertyKey:(NSString *)property andClass:(Class)class
{
NSString *key = [self uniqueKeyForClass:class andKey:property];
[self.dateFormatterDictionary setObject:dateFormatter forKey:key];

if (self.automaticallyGenerateInverseMapping)
{
[self setDateFormatter:dateFormatter forDictionaryKey:property andClass:class];
}
}

- (void)setDateFormatter:(NSDateFormatter *)dateFormatter forDictionaryKey:(NSString *)dictionaryKey andClass:(Class)class
{
NSString *key = [self uniqueKeyForClass:class andKey:dictionaryKey];
[self.inverseDateFormatterDictionary setObject:dateFormatter forKey:key];
}

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

- (NSDateFormatter *)dateFormatterForClass:(Class)class andProperty:(NSString *)property
- (ObjectMappingInfo *)mappingInfoForClass:(Class)class andPropertyKey:(NSString *)source {
NSString *key = [self uniqueKeyForClass:class andKey:source];
return [self.inverseMappingDictionary objectForKey:key];
}

- (NSDateFormatter *)dateFormatterForClass:(Class)class andPropertyKey:(NSString *)propertyKey
{
NSString *key = [self uniqueKeyForClass:class andKey:property];
NSString *key = [self uniqueKeyForClass:class andKey:propertyKey];
return [self.dateFormatterDictionary objectForKey:key];
}

- (NSDateFormatter *)dateFormatterForClass:(Class)class andDictionaryKey:(NSString *)dictionaryKey
{
NSString *key = [self uniqueKeyForClass:class andKey:dictionaryKey];
return [self.dateFormatterDictionary objectForKey:key];
}

Expand Down
4 changes: 3 additions & 1 deletion OCMapper/Source/Mapping Provider/MappingProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
@protocol MappingProvider <NSObject>

- (ObjectMappingInfo *)mappingInfoForClass:(Class)class andDictionaryKey:(NSString *)key;
- (NSDateFormatter *)dateFormatterForClass:(Class)class andProperty:(NSString *)property;
- (ObjectMappingInfo *)mappingInfoForClass:(Class)class andPropertyKey:(NSString *)key;
- (NSDateFormatter *)dateFormatterForClass:(Class)class andPropertyKey:(NSString *)key;
- (NSDateFormatter *)dateFormatterForClass:(Class)class andDictionaryKey:(NSString *)key;

@end

This file was deleted.

116 changes: 0 additions & 116 deletions OCMapper/Source/Mapping Provider/PLIST Mapping/PLISTMappingProvider.m

This file was deleted.

Loading

0 comments on commit 3a66439

Please sign in to comment.