Added Object Mapping block helpers to RKObjectManager and RKObjectMapping. These enable you to perform ad-hoc object mapping very easily. Extended RKObjectRouter to match on superclasses if no specific route is found. This is helpful when using mocked objects with frameworks like Kiwi. fixes #238

This commit is contained in:
Blake Watters
2011-07-23 23:04:16 -04:00
parent 06e2f6665d
commit 80366afa84
7 changed files with 197 additions and 6 deletions

View File

@@ -27,12 +27,12 @@
- (void)routeClass:(Class)class toResourcePath:(NSString*)resourcePath forMethodName:(NSString*)methodName {
NSString* className = NSStringFromClass(class);
if (nil == [_routes objectForKey:className]) {
if (nil == [_routes objectForKey:class]) {
NSMutableDictionary* dictionary = [NSMutableDictionary dictionary];
[_routes setObject:dictionary forKey:className];
[_routes setObject:dictionary forKey:class];
}
NSMutableDictionary* classRoutes = [_routes objectForKey:className];
NSMutableDictionary* classRoutes = [_routes objectForKey:class];
if ([classRoutes objectForKey:methodName]) {
[NSException raise:nil format:@"A route has already been registered for class '%@' and HTTP method '%@'", className, methodName];
}
@@ -75,8 +75,26 @@
- (NSString*)resourcePathForObject:(NSObject*)object method:(RKRequestMethod)method {
NSString* methodName = [self HTTPVerbForMethod:method];
NSString* className = NSStringFromClass([object class]);
NSDictionary* classRoutes = [_routes objectForKey:className];
NSString* className = NSStringFromClass([object class]);
NSDictionary* classRoutes = nil;
// Check for exact matches
for (Class possibleClass in _routes) {
if ([object isMemberOfClass:possibleClass]) {
classRoutes = [_routes objectForKey:possibleClass];
break;
}
}
// Check for superclass matches
if (! classRoutes) {
for (Class possibleClass in _routes) {
if ([object isKindOfClass:possibleClass]) {
classRoutes = [_routes objectForKey:possibleClass];
break;
}
}
}
NSString* resourcePath = nil;
if ((resourcePath = [classRoutes objectForKey:methodName])) {