From bbce5abfd4b7dfc3f48db96114a400209a062dd5 Mon Sep 17 00:00:00 2001 From: Andrew Newdel Date: Thu, 21 Apr 2011 19:24:41 -0400 Subject: [PATCH] Use the keyPath property to map a subset of the response for a single target object. --- Code/ObjectMapping/RKObjectLoader.m | 2 +- Code/ObjectMapping/RKObjectMapper.h | 1 + Code/ObjectMapping/RKObjectMapper.m | 10 +++++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Code/ObjectMapping/RKObjectLoader.m b/Code/ObjectMapping/RKObjectLoader.m index 5d00476d..0b60baf8 100644 --- a/Code/ObjectMapping/RKObjectLoader.m +++ b/Code/ObjectMapping/RKObjectLoader.m @@ -154,7 +154,7 @@ */ NSArray* results = nil; if (self.targetObject) { - [self.objectMapper mapObject:self.targetObject fromString:[response bodyAsString]]; + [self.objectMapper mapObject:self.targetObject fromString:[response bodyAsString] keyPath:_keyPath]; results = [NSArray arrayWithObject:self.targetObject]; } else { id result = [self.objectMapper mapFromString:[response bodyAsString] toClass:self.objectClass keyPath:_keyPath]; diff --git a/Code/ObjectMapping/RKObjectMapper.h b/Code/ObjectMapping/RKObjectMapper.h index 3b4c18e1..6db46c64 100644 --- a/Code/ObjectMapping/RKObjectMapper.h +++ b/Code/ObjectMapping/RKObjectMapper.h @@ -121,6 +121,7 @@ typedef enum { * provided */ - (void)mapObject:(id)model fromString:(NSString*)string; +- (void)mapObject:(id)model fromString:(NSString*)string keyPath:(NSString*)keyPath; /////////////////////////////////////////////////////////////////////////////// // Object Mapping API diff --git a/Code/ObjectMapping/RKObjectMapper.m b/Code/ObjectMapping/RKObjectMapper.m index 1f8c8c97..3c438392 100644 --- a/Code/ObjectMapping/RKObjectMapper.m +++ b/Code/ObjectMapping/RKObjectMapper.m @@ -163,8 +163,12 @@ static const NSString* kRKModelMapperRailsDateFormatString = @"MM/dd/yyyy"; return [self mapFromString:string toClass:nil keyPath:nil]; } -- (void)mapObject:(NSObject*)model fromString:(NSString*)string { +- (void)mapObject:(NSObject*)model fromString:(NSString*)string keyPath:(NSString*)keyPath { id object = [self parseString:string]; + if (keyPath) { + object = [object valueForKeyPath:keyPath]; + } + if ([object isKindOfClass:[NSDictionary class]]) { [self mapObject:model fromDictionary:object]; } else if (nil == object) { @@ -176,6 +180,10 @@ static const NSString* kRKModelMapperRailsDateFormatString = @"MM/dd/yyyy"; } } +- (void)mapObject:(NSObject*)model fromString:(NSString*)string { + [self mapObject:model fromString:string keyPath:nil]; +} + /////////////////////////////////////////////////////////////////////////////// // Mapping from objects