Refactored object loaders to provide easier support for non-nested JSON responses. There are new flavors of getObject:, postObject:, etc.

that allow the developer to explicitly specify the object mapping to use for processing the response. closes #168
This commit is contained in:
Blake Watters
2011-06-30 09:37:53 -04:00
parent e18c1f2811
commit 2ac45f5ccc
12 changed files with 188 additions and 79 deletions

View File

@@ -273,4 +273,27 @@
assertThat([loader.mappableData valueForKey:@"newKey"], is(equalTo(@"monkey!")));
}
- (void)itShouldAllowYouToPOSTAnObjectAndMapBackNonNestedContent {
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RKSpecComplexUser class]];
[mapping mapAttributes:@"firstname", @"lastname", @"email", nil];
RKObjectMapping* serializationMapping = [mapping inverseMapping];
RKObjectManager* objectManager = RKSpecNewObjectManager();
[objectManager.router routeClass:[RKSpecComplexUser class] toResourcePath:@"/notNestedUser"];
[objectManager.mappingProvider setSerializationMapping:serializationMapping forClass:[RKSpecComplexUser class]];
RKSpecComplexUser* user = [[RKSpecComplexUser new] autorelease];
user.firstname = @"Blake";
user.lastname = @"Watters";
user.email = @"blake@restkit.org";
RKSpecResponseLoader* responseLoader = [RKSpecResponseLoader responseLoader];
RKObjectLoader* loader = [objectManager objectLoaderForObject:user method:RKRequestMethodPOST delegate:responseLoader];
loader.objectMapping = mapping;
[loader send];
[responseLoader waitForResponse];
assertThatBool([responseLoader success], is(equalToBool(YES)));
assertThat(user.email, is(equalTo(@"changed")));
}
@end

View File

@@ -94,14 +94,16 @@
- (void)itShouldDeleteACoreDataBackedTargetObjectOnError {
RKHuman* temporaryHuman = [[RKHuman alloc] initWithEntity:[NSEntityDescription entityForName:@"RKHuman" inManagedObjectContext:_objectManager.objectStore.managedObjectContext] insertIntoManagedObjectContext:_objectManager.objectStore.managedObjectContext];
temporaryHuman.name = @"My Name";
// [_objectManager.objectStore save];
temporaryHuman.name = @"My Name";
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping mapAttributes:@"name", nil];
RKSpecResponseLoader* loader = [RKSpecResponseLoader responseLoader];
NSString* resourcePath = @"/humans/fail";
RKObjectLoader* objectLoader = [_objectManager objectLoaderWithResourcePath:resourcePath delegate:loader];
objectLoader.method = RKRequestMethodPOST;
objectLoader.targetObject = temporaryHuman;
objectLoader.serializationMapping = mapping;
[objectLoader send];
[loader waitForResponse];
@@ -111,6 +113,8 @@
- (void)itShouldNotDeleteACoreDataBackedTargetObjectOnErrorIfItWasAlreadySaved {
RKHuman* temporaryHuman = [[RKHuman alloc] initWithEntity:[NSEntityDescription entityForName:@"RKHuman" inManagedObjectContext:_objectManager.objectStore.managedObjectContext] insertIntoManagedObjectContext:_objectManager.objectStore.managedObjectContext];
temporaryHuman.name = @"My Name";
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping mapAttributes:@"name", nil];
// Save it to suppress deletion
[_objectManager.objectStore save];
@@ -120,6 +124,7 @@
RKObjectLoader* objectLoader = [_objectManager objectLoaderWithResourcePath:resourcePath delegate:loader];
objectLoader.method = RKRequestMethodPOST;
objectLoader.targetObject = temporaryHuman;
objectLoader.serializationMapping = mapping;
[objectLoader send];
[loader waitForResponse];