mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-24 04:46:01 +08:00
Implemented RKMakePathWithObject. Cleaned up some TODO's
This commit is contained in:
@@ -34,6 +34,18 @@ NSURL* RKMakeURL(NSString* resourcePath);
|
||||
*/
|
||||
NSString* RKMakeURLPath(NSString* resourcePath);
|
||||
|
||||
/**
|
||||
* Convenience method for generating a path against the properties of an object. Takes
|
||||
* a string with property names encoded in parentheses and interpolates the values of
|
||||
* the properties specified and returns the generated path.
|
||||
*
|
||||
* For example, given an 'article' object with an 'articleID' property of 12345
|
||||
* RKMakePathWithObject(@"articles/(articleID)", article) would generate @"articles/12345"
|
||||
*
|
||||
* This functionality is the basis for resource path generation in the Router.
|
||||
*/
|
||||
NSString* RKMakePathWithObject(NSString* path, id object);
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,6 +27,42 @@ NSString* RKMakeURLPath(NSString* resourcePath) {
|
||||
return [[RKClient sharedClient] URLPathForResourcePath:resourcePath];
|
||||
}
|
||||
|
||||
NSString* RKMakePathWithObject(NSString* path, id object) {
|
||||
NSMutableDictionary* substitutions = [NSMutableDictionary dictionary];
|
||||
NSScanner* scanner = [NSScanner scannerWithString:path];
|
||||
|
||||
BOOL startsWithParentheses = [[path substringToIndex:1] isEqualToString:@"("];
|
||||
while ([scanner isAtEnd] == NO) {
|
||||
NSString* keyPath = nil;
|
||||
if (startsWithParentheses || [scanner scanUpToString:@"(" intoString:nil]) {
|
||||
// Advance beyond the opening parentheses
|
||||
if (NO == [scanner isAtEnd]) {
|
||||
[scanner setScanLocation:[scanner scanLocation] + 1];
|
||||
}
|
||||
if ([scanner scanUpToString:@")" intoString:&keyPath]) {
|
||||
NSString* searchString = [NSString stringWithFormat:@"(%@)", keyPath];
|
||||
NSString* propertyStringValue = [NSString stringWithFormat:@"%@", [object valueForKeyPath:keyPath]];
|
||||
[substitutions setObject:propertyStringValue forKey:searchString];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (0 == [substitutions count]) {
|
||||
return path;
|
||||
}
|
||||
|
||||
NSMutableString* interpolatedPath = [[path mutableCopy] autorelease];
|
||||
for (NSString* find in substitutions) {
|
||||
NSString* replace = [substitutions valueForKey:find];
|
||||
[interpolatedPath replaceOccurrencesOfString:find
|
||||
withString:replace
|
||||
options:NSLiteralSearch
|
||||
range:NSMakeRange(0, [interpolatedPath length])];
|
||||
}
|
||||
|
||||
return [NSString stringWithString:interpolatedPath];
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@implementation RKClient
|
||||
|
||||
@@ -74,40 +74,6 @@
|
||||
|
||||
#pragma mark RKRouter
|
||||
|
||||
- (NSString*)resourcePath:(NSString*)resourcePath withPropertiesInterpolatedForObject:(NSObject<RKObjectMappable>*)object {
|
||||
NSMutableDictionary* substitutions = [NSMutableDictionary dictionary];
|
||||
NSScanner* scanner = [NSScanner scannerWithString:resourcePath];
|
||||
|
||||
BOOL startsWithParentheses = [[resourcePath substringToIndex:1] isEqualToString:@"("];
|
||||
while ([scanner isAtEnd] == NO) {
|
||||
NSString* keyPath = nil;
|
||||
if (startsWithParentheses || [scanner scanUpToString:@"(" intoString:nil]) {
|
||||
// Advance beyond the opening parentheses
|
||||
if (NO == [scanner isAtEnd]) {
|
||||
[scanner setScanLocation:[scanner scanLocation] + 1];
|
||||
}
|
||||
if ([scanner scanUpToString:@")" intoString:&keyPath]) {
|
||||
NSString* searchString = [NSString stringWithFormat:@"(%@)", keyPath];
|
||||
NSString* propertyStringValue = [NSString stringWithFormat:@"%@", [object valueForKeyPath:keyPath]];
|
||||
[substitutions setObject:propertyStringValue forKey:searchString];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (0 == [substitutions count]) {
|
||||
return resourcePath;
|
||||
}
|
||||
|
||||
NSMutableString* interpolatedResourcePath = [[resourcePath mutableCopy] autorelease];
|
||||
for (NSString* find in substitutions) {
|
||||
NSString* replace = [substitutions valueForKey:find];
|
||||
[interpolatedResourcePath replaceOccurrencesOfString:find withString:replace
|
||||
options:NSLiteralSearch range:NSMakeRange(0, [interpolatedResourcePath length])];
|
||||
}
|
||||
|
||||
return [NSString stringWithString:interpolatedResourcePath];
|
||||
}
|
||||
|
||||
- (NSString*)resourcePathForObject:(NSObject<RKObjectMappable>*)object method:(RKRequestMethod)method {
|
||||
NSString* methodName = [self HTTPVerbForMethod:method];
|
||||
NSString* className = NSStringFromClass([object class]);
|
||||
@@ -115,11 +81,11 @@
|
||||
|
||||
NSString* resourcePath = nil;
|
||||
if (resourcePath = [classRoutes objectForKey:methodName]) {
|
||||
return [self resourcePath:resourcePath withPropertiesInterpolatedForObject:object];
|
||||
return RKMakePathWithObject(resourcePath, object);
|
||||
}
|
||||
|
||||
if (resourcePath = [classRoutes objectForKey:@"ANY"]) {
|
||||
return [self resourcePath:resourcePath withPropertiesInterpolatedForObject:object];
|
||||
return RKMakePathWithObject(resourcePath, object);
|
||||
}
|
||||
|
||||
[NSException raise:nil format:@"Unable to find a routable path for object of type '%@' for HTTP Method '%@'", className, methodName];
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
- (void)viewDidUnload {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self name:DBUserDidLoginNotification object:nil];
|
||||
// [[NSNotificationCenter defaultCenter] removeObserver:self name:kLoginCanceledNotificationName object:nil];
|
||||
}
|
||||
|
||||
- (void)loadView {
|
||||
@@ -60,5 +59,4 @@
|
||||
[self.navigationController popViewControllerAnimated:YES];
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
|
||||
@@ -17,8 +17,8 @@
|
||||
if (self = [super initWithStyle:UITableViewStylePlain]) {
|
||||
_topicID = [topicID retain];
|
||||
self.title = @"Posts";
|
||||
// TODO: Use routing or something to generate this URL. RKMakePathWithObject
|
||||
_resourcePath = [[NSString stringWithFormat:@"/topics/%@/posts", _topicID] retain];
|
||||
_resourcePath = RKMakePathWithObject(@"/topics/(topicID)/posts", self.topic);
|
||||
[_resourcePath retain];
|
||||
_resourceClass = [DBPost class];
|
||||
}
|
||||
return self;
|
||||
@@ -42,10 +42,8 @@
|
||||
}
|
||||
|
||||
- (void)addButtonWasPressed:(id)sender {
|
||||
// TODO: Move this onto the model...
|
||||
// RKMakeObjectPath / RKMakePathWithObject(@"db://topics/(topicID)/posts/new", self.topic);
|
||||
NSString* url = [NSString stringWithFormat:@"db://topics/%@/posts/new", _topicID];
|
||||
TTOpenURL(url);
|
||||
NSString* URLString = RKMakePathWithObject(@"db://topics/(topicID)/posts/new", self.topic);
|
||||
TTOpenURL(URLString);
|
||||
}
|
||||
|
||||
- (void)createModel {
|
||||
@@ -72,6 +70,7 @@
|
||||
// the current user id == topic user id.
|
||||
NSNumber* topicUserId = self.topic.userID;
|
||||
// if topicUserId is nil, the topic has no user for some reason (perhaps they got deleted).
|
||||
// TODO: Permissions pattern
|
||||
if ([DBUser currentUser] == nil ||
|
||||
(topicUserId && [[DBUser currentUser].userID isEqualToNumber:topicUserId])) {
|
||||
NSString* editURL = [NSString stringWithFormat:@"db://topics/%@/edit", _topicID];
|
||||
|
||||
@@ -58,9 +58,8 @@
|
||||
NSMutableArray* items = [NSMutableArray arrayWithCapacity:[model.objects count]];
|
||||
|
||||
for(DBTopic* topic in model.objects) {
|
||||
// TODO: RKMakePathWithObject. Move to postsTTURL method?
|
||||
NSString* url = [NSString stringWithFormat:@"db://topics/%@/posts", topic.topicID];
|
||||
[items addObject:[TTTableTextItem itemWithText:topic.name URL:url]];
|
||||
NSString* topicPostsURL = RKMakePathWithObject(@"db://topics/(topicID)/posts", topic);
|
||||
[items addObject:[TTTableTextItem itemWithText:topic.name URL:topicPostsURL]];
|
||||
}
|
||||
|
||||
NSLog(@"Items: %@", items);
|
||||
|
||||
Reference in New Issue
Block a user