mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-23 04:20:21 +08:00
Added basic specs covering the Rails router. Added some documentation to the headers
This commit is contained in:
@@ -9,8 +9,20 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "RKDynamicRouter.h"
|
||||
|
||||
/**
|
||||
* An implementation of the RKRouter protocol suitable for interacting
|
||||
* with a Ruby on Rails backend service. This router implementation extends
|
||||
* the normal dynamic router and provides support for encoding properties in
|
||||
* such a way that Rails controllers expect (i.e. model_name[attribute])
|
||||
*/
|
||||
@interface RKRailsRouter : RKDynamicRouter {
|
||||
|
||||
NSMutableDictionary* _classToModelMappings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the remote model name for a local domain class. This model name will
|
||||
* be used when serializing parameters before dispatching the request
|
||||
*/
|
||||
- (void)setModelName:(NSString*)modelName forClass:(Class<RKObjectMappable>)class;
|
||||
|
||||
@end
|
||||
|
||||
@@ -7,8 +7,63 @@
|
||||
//
|
||||
|
||||
#import "RKRailsRouter.h"
|
||||
|
||||
#import "../Support/NSString+InflectionSupport.h"
|
||||
|
||||
@implementation RKRailsRouter
|
||||
|
||||
- (id)init {
|
||||
if (self = [super init]) {
|
||||
_classToModelMappings = [[NSMutableDictionary alloc] init];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[_classToModelMappings release];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (void)setModelName:(NSString*)modelName forClass:(Class<RKObjectMappable>)class {
|
||||
[_classToModelMappings setObject:modelName forKey:class];
|
||||
}
|
||||
|
||||
- (NSDictionary*)elementNamesAndPropertyValuesForObject:(NSObject<RKObjectMappable>*)object {
|
||||
NSDictionary* mappings = [[object class] elementToPropertyMappings];
|
||||
NSMutableDictionary* elementsAndPropertyValues = [NSMutableDictionary dictionaryWithCapacity:[mappings count]];
|
||||
// Return all the properties of this model in a dictionary under their element names
|
||||
for (NSString* elementName in mappings) {
|
||||
NSString* propertyName = [mappings valueForKey:elementName];
|
||||
id propertyValue = [object valueForKey:propertyName];
|
||||
[elementsAndPropertyValues setValue:propertyValue forKey:elementName];
|
||||
}
|
||||
|
||||
return (NSDictionary*) elementsAndPropertyValues;
|
||||
}
|
||||
|
||||
#pragma mark RKRouter
|
||||
|
||||
- (NSObject<RKRequestSerializable>*)serializationForObject:(NSObject<RKObjectMappable>*)object method:(RKRequestMethod)method {
|
||||
NSDictionary* elementsAndProperties = [self elementNamesAndPropertyValuesForObject:object];
|
||||
NSMutableDictionary* resourceParams = [NSMutableDictionary dictionaryWithCapacity:[elementsAndProperties count]];
|
||||
NSString* modelName = [_classToModelMappings objectForKey:[object class]];
|
||||
if (nil == modelName) {
|
||||
NSString* className = NSStringFromClass([object class]);
|
||||
[NSException raise:nil format:@"Unable to find registered modelName for class '%@'", className];
|
||||
}
|
||||
|
||||
NSString* underscoredModelName = [modelName underscore];
|
||||
|
||||
for (NSString* elementName in [elementsAndProperties allKeys]) {
|
||||
id value = [elementsAndProperties valueForKey:elementName];
|
||||
NSString* attributeName = [elementName stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
|
||||
if (![attributeName isEqualToString:@"id"]) {
|
||||
NSString* keyName = [NSString stringWithFormat:@"%@[%@]", underscoredModelName, attributeName];
|
||||
[resourceParams setValue:value forKey:keyName];
|
||||
}
|
||||
}
|
||||
|
||||
return resourceParams;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user