Added support for polymorphic object mapping (Github #105, #244). This enables you to dynamically map objects to different destination classes or using different mapping strategies via configuration or callbacks. See Docs/Object Mapping.md for details.

Other changes include:
* Eliminated the RKObjectFactory protocol and implementations. Object mapping instances themselves are
now responsible for instantiating target objects for mapping.
* Introduced RKObjectAbstractMapping superclass for RKObjectMapping and RKObjectPolymorphicMapping.
* Updated example applications to use block object loaders (RKTwitter and RKTwitterCoreData)
* Refactored method signatures of RKObjectMapper, RKObjectMapping, and RKObjectMappingProvider to reflect the
existence of abstract mapping types. This was necessary to make polymorphic mappings integrate cleanly.
* Fixed overlap in RestKit error domains between network and object mapping. fixes #208
This commit is contained in:
Blake Watters
2011-07-30 16:00:36 -04:00
parent 072e1ee58b
commit 670234b775
64 changed files with 1411 additions and 601 deletions

View File

@@ -13,7 +13,7 @@
- (id)init {
if ((self = [super init])) {
_objectMappings = [NSMutableArray new];
_objectMappingsByKeyPath = [NSMutableDictionary new];
_mappingsByKeyPath = [NSMutableDictionary new];
_serializationMappings = [NSMutableDictionary new];
}
return self;
@@ -21,21 +21,17 @@
- (void)dealloc {
[_objectMappings release];
[_objectMappingsByKeyPath release];
[_mappingsByKeyPath release];
[_serializationMappings release];
[super dealloc];
}
- (RKObjectMapping*)objectMappingForKeyPath:(NSString*)keyPath {
return [_objectMappingsByKeyPath objectForKey:keyPath];
}
- (void)setMapping:(RKObjectMapping*)mapping forKeyPath:(NSString*)keyPath {
[_objectMappingsByKeyPath setValue:mapping forKey:keyPath];
[_mappingsByKeyPath setValue:mapping forKey:keyPath];
}
- (void)setObjectMapping:(RKObjectMapping*)mapping forKeyPath:(NSString*)keyPath {
[_objectMappingsByKeyPath setValue:mapping forKey:keyPath];
- (RKObjectAbstractMapping*)mappingForKeyPath:(NSString*)keyPath {
return [_mappingsByKeyPath objectForKey:keyPath];
}
- (void)setSerializationMapping:(RKObjectMapping *)mapping forClass:(Class)objectClass {
@@ -46,14 +42,14 @@
return (RKObjectMapping*)[_serializationMappings objectForKey:NSStringFromClass(objectClass)];
}
- (NSDictionary*)objectMappingsByKeyPath {
return _objectMappingsByKeyPath;
- (NSDictionary*)mappingsByKeyPath {
return _mappingsByKeyPath;
}
- (void)registerMapping:(RKObjectMapping*)objectMapping withRootKeyPath:(NSString*)keyPath {
// TODO: Should generate logs
objectMapping.rootKeyPath = keyPath;
[self setObjectMapping:objectMapping forKeyPath:keyPath];
[self setMapping:objectMapping forKeyPath:keyPath];
RKObjectMapping* inverseMapping = [objectMapping inverseMapping];
inverseMapping.rootKeyPath = keyPath;
[self setSerializationMapping:inverseMapping forClass:objectMapping.objectClass];
@@ -65,7 +61,7 @@
- (NSArray*)objectMappingsForClass:(Class)theClass {
NSMutableArray* mappings = [NSMutableArray array];
NSArray* mappingsToSearch = [[NSArray arrayWithArray:_objectMappings] arrayByAddingObjectsFromArray:[_objectMappingsByKeyPath allValues]];
NSArray* mappingsToSearch = [[NSArray arrayWithArray:_objectMappings] arrayByAddingObjectsFromArray:[_mappingsByKeyPath allValues]];
for (RKObjectMapping* objectMapping in mappingsToSearch) {
if (objectMapping.objectClass == theClass && ![mappings containsObject:objectMapping]) {
[mappings addObject:objectMapping];
@@ -80,4 +76,18 @@
return ([objectMappings count] > 0) ? [objectMappings objectAtIndex:0] : nil;
}
#pragma mark - Deprecated
- (RKObjectMapping*)objectMappingForKeyPath:(NSString*)keyPath {
return (RKObjectMapping*) [self mappingForKeyPath:keyPath];
}
- (void)setObjectMapping:(RKObjectMapping*)mapping forKeyPath:(NSString*)keyPath {
[self setMapping:mapping forKeyPath:keyPath];
}
- (NSDictionary*)objectMappingsByKeyPath {
return [self mappingsByKeyPath];
}
@end