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 b5fa38d
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 192 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,13 @@

@interface InCodeMappingProvider : NSObject <MappingProvider>

@property (nonatomic, assign) BOOL automaticallyGenerateInverseMapping;

- (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *)propertyKey withObjectType:(Class)objectType forClass:(Class)class;
- (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *)propertyKey forClass:(Class)class withTransformer:(MappingTransformer)transformer;
- (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *)propertyKey forClass:(Class)class;
- (void)mapFromPropertyKey:(NSString *)propertyKey toDictionaryKey:(NSString *)dictionaryKey forClass:(Class)class;
- (void)mapFromPropertyKey:(NSString *)propertyKey toDictionaryKey:(NSString *)dictionaryKey forClass:(Class)class withTransformer:(MappingTransformer)transformer;
- (void)setDateFormatter:(NSDateFormatter *)dateFormatter forProperty:(NSString *)property andClass:(Class)class;

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

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

Expand All @@ -44,7 +45,9 @@ - (id)init
{
if (self = [super init])
{
self.automaticallyGenerateInverseMapping = YES;
self.mappingDictionary = [NSMutableDictionary dictionary];
self.inverseMappingDictionary = [NSMutableDictionary dictionary];
self.dateFormatterDictionary = [NSMutableDictionary dictionary];
}

Expand All @@ -58,6 +61,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,6 +80,18 @@ - (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *
[self.mappingDictionary setObject:info forKey:key];
}

- (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 forProperty:(NSString *)property andClass:(Class)class
{
NSString *key = [self uniqueKeyForClass:class andKey:property];
Expand All @@ -93,6 +113,11 @@ - (ObjectMappingInfo *)mappingInfoForClass:(Class)class andDictionaryKey:(NSStri
return [self.mappingDictionary objectForKey:key];
}

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

- (NSDateFormatter *)dateFormatterForClass:(Class)class andProperty:(NSString *)property
{
NSString *key = [self uniqueKeyForClass:class andKey:property];
Expand Down
1 change: 1 addition & 0 deletions OCMapper/Source/Mapping Provider/MappingProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@protocol MappingProvider <NSObject>

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

@end

This file was deleted.

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

This file was deleted.

12 changes: 9 additions & 3 deletions OCMapper/Source/ObjectMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,16 @@ - (NSDictionary *)processDictionaryFromObject:(NSObject *)object
NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];
Class class = NSClassFromString([self typeForProperty:propertyName andClass:[object class]]);
id propertyValue = [object valueForKey:(NSString *)propertyName];

ObjectMappingInfo *mapingInfo = [self.mappingProvider mappingInfoForClass:[object class] andPropertyKey:propertyName];
propertyName = (mapingInfo) ? mapingInfo.dictionaryKey : propertyName;

if (mapingInfo.transformer) {
propertyValue = mapingInfo.transformer(propertyValue, object);
[props setObject:propertyValue forKey:propertyName];
}
// If class is in the main bundle it's an application specific class
if ([NSBundle mainBundle] == [NSBundle bundleForClass:[propertyValue class]])
else if ([NSBundle mainBundle] == [NSBundle bundleForClass:[propertyValue class]])
{
if (propertyValue) [props setObject:[self dictionaryFromObject:propertyValue] forKey:propertyName];
}
Expand All @@ -197,8 +204,7 @@ - (NSDictionary *)processDictionaryFromObject:(NSObject *)object
{
propertyValue = [self processDictionaryFromArray:propertyValue];
}



if (propertyValue) [props setObject:propertyValue forKey:propertyName];
}
}
Expand Down
1 change: 0 additions & 1 deletion OCMapperTests/ObjectMapperTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#import "ObjectMapper.h"
#import "InCodeMappingProvider.h"
#import "ObjectInstanceProvider.h"
#import "PLISTMappingProvider.h"
#import "CommonLoggingProvider.h"

@interface ObjectMapperTests : XCTestCase
Expand Down
Loading

0 comments on commit b5fa38d

Please sign in to comment.