Eliminate rootKeyPath from RKMapping. Now encapsulated into RKRequestDescriptor.

This commit is contained in:
Blake Watters
2012-08-28 13:42:39 -04:00
parent d926e240f8
commit 0484714a00
5 changed files with 18 additions and 47 deletions

View File

@@ -25,19 +25,6 @@
*/
@interface RKMapping : NSObject
/**
The root key path for the receiver.
Root key paths are handled differently depending on the context in which the mapping is
being used. If the receiver is used for object mapping, the rootKeyPath specifies a nested
root dictionary that all attribute and relationship mappings will be considered relative to. When
the mapping is used in a serialization context, the rootKeyPath specifies that the serialized content
should be stored in a dictionary nested with the rootKeyPath as the key.
@see RKObjectSerializer
*/
@property (nonatomic, copy) NSString *rootKeyPath;
/**
Forces the mapper to treat the mapped keyPath as a collection even if it does not
return an array or a set of objects. This permits mapping where a dictionary identifies

View File

@@ -10,7 +10,6 @@
@implementation RKMapping
@synthesize rootKeyPath;
@synthesize forceCollectionMapping;
- (BOOL)isEqualToMapping:(RKMapping *)otherMapping

View File

@@ -265,8 +265,7 @@
RKMapping *configuredObjectMapping = [self configuredObjectMapping];
if (configuredObjectMapping) {
mappingProvider = [RKObjectMappingProvider mappingProvider];
NSString *rootKeyPath = configuredObjectMapping.rootKeyPath ? configuredObjectMapping.rootKeyPath : @"";
[mappingProvider setMapping:configuredObjectMapping forKeyPath:rootKeyPath];
[mappingProvider setMapping:configuredObjectMapping forKeyPath:@""];
// Copy the error mapping from our configured mappingProvider
mappingProvider.errorMapping = self.mappingProvider.errorMapping;
@@ -377,7 +376,7 @@
if ((self.sourceObject && self.params == nil) && (self.method == RKRequestMethodPOST || self.method == RKRequestMethodPUT)) {
NSAssert(self.serializationMapping, @"You must provide a serialization mapping for objects of type '%@'", NSStringFromClass([self.sourceObject class]));
RKLogDebug(@"POST or PUT request for source object %@, serializing to MIME Type %@ for transport...", self.sourceObject, self.serializationMIMEType);
RKObjectSerializer *serializer = [RKObjectSerializer serializerWithObject:self.sourceObject mapping:self.serializationMapping];
RKObjectSerializer *serializer = [[[RKObjectSerializer alloc] initWithObject:self.sourceObject mapping:self.serializationMapping rootKeyPath:nil] autorelease];
NSError *error = nil;
id params = [serializer serializationForMIMEType:self.serializationMIMEType error:&error];

View File

@@ -30,11 +30,11 @@
*/
@interface RKObjectSerializer : NSObject <RKMappingOperationDelegate>
@property (nonatomic, readonly) id object;
@property (nonatomic, readonly) RKObjectMapping *mapping;
@property (nonatomic, readonly, strong) id object;
@property (nonatomic, readonly, strong) RKObjectMapping *mapping;
@property (nonatomic, readonly, strong) NSString *rootKeyPath;
+ (id)serializerWithObject:(id)object mapping:(RKObjectMapping *)mapping;
- (id)initWithObject:(id)object mapping:(RKObjectMapping *)mapping;
- (id)initWithObject:(id)object mapping:(RKObjectMapping *)mapping rootKeyPath:(NSString *)rootKeyPath;
/**
Return a serialized representation of the source object by applying an object mapping

View File

@@ -30,38 +30,29 @@
#undef RKLogComponent
#define RKLogComponent lcl_cRestKitObjectMapping
@interface RKObjectSerializer ()
@property (nonatomic, readwrite, strong) id object;
@property (nonatomic, readwrite, strong) RKObjectMapping *mapping;
@property (nonatomic, readwrite, strong) NSString *rootKeyPath;
@end
@implementation RKObjectSerializer
@synthesize object = _object;
@synthesize mapping = _mapping;
+ (id)serializerWithObject:(id)object mapping:(RKObjectMapping *)mapping
{
return [[[self alloc] initWithObject:object mapping:mapping] autorelease];
}
- (id)initWithObject:(id)object mapping:(RKObjectMapping *)mapping
- (id)initWithObject:(id)object mapping:(RKObjectMapping *)mapping rootKeyPath:(NSString *)rootKeyPath;
{
self = [super init];
if (self) {
_object = [object retain];
_mapping = [mapping retain];
self.object = object;
self.mapping = mapping;
self.rootKeyPath = rootKeyPath;
}
return self;
}
- (void)dealloc
{
[_object release];
[_mapping release];
[super dealloc];
}
- (id)serializeObjectToDictionary:(NSError **)error
{
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
RKMappingOperation *operation = [RKMappingOperation mappingOperationFromObject:_object toObject:dictionary withMapping:_mapping];
RKMappingOperation *operation = [RKMappingOperation mappingOperationFromObject:self.object toObject:dictionary withMapping:self.mapping];
operation.delegate = self;
BOOL success = [operation performMapping:error];
if (!success) {
@@ -69,12 +60,7 @@
}
// Optionally enclose the serialized object within a container...
if (_mapping.rootKeyPath) {
// TODO: Should log this...
dictionary = [NSMutableDictionary dictionaryWithObject:dictionary forKey:_mapping.rootKeyPath];
}
return dictionary;
return self.rootKeyPath ? [NSMutableDictionary dictionaryWithObject:dictionary forKey:self.rootKeyPath] : dictionary;
}
- (id)serializeObjectToMIMEType:(NSString *)MIMEType error:(NSError **)error