Added basic specs covering the Rails router. Added some documentation to the headers

This commit is contained in:
Blake Watters
2010-10-19 19:20:08 -04:00
parent e5100bd7e5
commit 724091b5a1
4 changed files with 131 additions and 8 deletions

View File

@@ -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

View File

@@ -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