Merge branch 'master' of github.com:twotoasters/RestKit

This commit is contained in:
jeremy@twotoasters.com
2010-03-18 13:07:23 -04:00
8 changed files with 80 additions and 30 deletions

View File

@@ -7,6 +7,9 @@
//
#import "RKManagedObjectStore.h"
#import <UIKit/UIKit.h>
NSString* const RKManagedObjectStoreDidFailSaveNotification = @"RKManagedObjectStoreDidFailSaveNotification";
@interface RKManagedObjectStore (Private)
- (void)createPersistentStoreCoordinator;
@@ -45,11 +48,27 @@
message to the application's managed object context.
*/
- (NSError*)save {
NSError *error;
if (NO == [[self managedObjectContext] save:&error]) {
NSError *error = nil;
@try {
[[self managedObjectContext] save:&error];
}
@catch (NSException* e) {
// TODO: This needs to be reworked into a delegation pattern
NSString* errorMessage = [NSString stringWithFormat:@"An unrecoverable error was encountered while trying to save the database: %@", [e reason]];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Ruh roh.", nil)
message:errorMessage
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", nil)
otherButtonTitles:nil];
[alert show];
[alert release];
}
@finally {
if (error) {
NSDictionary* userInfo = [NSDictionary dictionaryWithObject:error forKey:@"error"];
[[NSNotificationCenter defaultCenter] postNotificationName:RKManagedObjectStoreDidFailSaveNotification object:self userInfo:userInfo];
}
return error;
} else {
return nil;
}
}
@@ -65,7 +84,7 @@
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
// Handle the error.
// TODO: Needs to be handled with delegation... Allow the application to handle migration.
}
}
@@ -94,8 +113,7 @@
/**
Returns the path to the application's documents directory.
*/
- (NSString *)applicationDocumentsDirectory {
- (NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;

View File

@@ -7,9 +7,10 @@
//
#import "RKModelMapper.h"
#import "JSON.h"
@interface RKMappingFormatJSONParser : NSObject <RKMappingFormatParser> {
SBJSON* _parser;
}
@end

View File

@@ -7,12 +7,30 @@
//
#import "RKMappingFormatJSONParser.h"
#import "JSON.h"
@implementation RKMappingFormatJSONParser
- (id)init {
if (self = [super init]) {
_parser = [[SBJSON alloc] init];
}
return self;
}
- (void)dealloc {
[_parser release];
[super dealloc];
}
- (NSDictionary*)objectFromString:(NSString*)string {
return [[[[SBJSON alloc] init] autorelease] objectWithString:string];
id result = [_parser objectWithString:string];
if (nil == result) {
// TODO: Need to surface these errors in a better fashion
NSLog(@"[RestKit] RKMappingFormatJSONParser: Parser failed with error trace: %@", _parser.errorTrace);
}
return result;
}
@end

View File

@@ -128,7 +128,7 @@ static RKModelManager* sharedManager = nil;
loader.delegate = delegate;
loader.callback = callback;
return [_client post:resourcePath params:params delegate:loader callback:loader.collectionCallback];
return [_client get:resourcePath params:params delegate:loader callback:loader.collectionCallback];
}
- (RKRequest*)getModel:(id<RKModelMappable>)model delegate:(NSObject<RKModelLoaderDelegate>*)delegate callback:(SEL)callback {

View File

@@ -268,7 +268,14 @@ static const NSString* kRKModelMapperRailsDateFormatString = @"MM/dd/yyyy";
for (NSString* elementKeyPath in elementToRelationshipMappings) {
NSString* propertyName = [elementToRelationshipMappings objectForKey:elementKeyPath];
id relationshipElements = [elements valueForKeyPath:elementKeyPath];
id relationshipElements = nil;
@try {
relationshipElements = [elements valueForKeyPath:elementKeyPath];
}
@catch (NSException* e) {
NSLog(@"Caught exception:%@ when trying valueForKeyPath with path:%@ for elements:%@", e, elementKeyPath, elements);
}
if ([relationshipElements isKindOfClass:[NSArray class]]) {
// NOTE: The last part of the keyPath contains the elementName for the mapped destination class of our children
NSArray* componentsOfKeyPath = [elementKeyPath componentsSeparatedByString:@"."];
@@ -301,8 +308,9 @@ static const NSString* kRKModelMapperRailsDateFormatString = @"MM/dd/yyyy";
- (NSDate*)parseDateFromString:(NSString*)string {
NSDate* date = nil;
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:self.remoteTimeZone];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
// TODO: I changed this to local time and it fixes my date issues. wtf?
[formatter setTimeZone:self.localTimeZone];
for (NSString* formatString in self.dateFormats) {
[formatter setDateFormat:formatString];
date = [formatter dateFromString:string];

View File

@@ -8,6 +8,13 @@
#import <CoreData/CoreData.h>
/**
* Notifications
*/
extern NSString* const RKManagedObjectStoreDidFailSaveNotification;
///////////////////////////////////////////////////////////////////
@interface RKManagedObjectStore : NSObject {
NSString* _storeFilename;
NSManagedObjectModel* _managedObjectModel;

View File

@@ -64,8 +64,9 @@ typedef enum {
- (NSString*)memberPath;
/**
* Must return the put/post params for the instance
* Must return the put/post params for the instance
*/
// TODO: Gets eliminated...
- (NSDictionary*)resourceParams;
/**
@@ -88,19 +89,4 @@ typedef enum {
*/
+ (id)findByPrimaryKey:(id)value;
/**
* Callback used to allow transformation of element name formatting before model mapping
* occurs. This can be used to change the formatting of elements. For example, Ruby on Rails
* formats XML element names hyphenated (my-attribute) and JSON element names underscored (my_attribute).
*/
+ (NSString*)formatElementName:(NSString*)elementName forMappingFormat:(RKMappingFormat)format;
/**
* If implemented, the model mapper will hand the xml to the object instead of mapping it using
* elementToRelationshipMappings and elementToPropertyMappings
*/
- (void)digestXML:(Element*)e;
- (void)digestJSONDictionary:(NSDictionary*)dict;
@end

View File

@@ -55,7 +55,19 @@
*/
@property(nonatomic, retain) NSArray* dateFormats;
/**
* The time zone of the remote system. Date strings pulled from the remote source
* will be considered to be local to this time zone when mapping.
*
* Defaults to UTC
*/
@property(nonatomic, retain) NSTimeZone* remoteTimeZone;
/**
* The target time zone to map dates to.
*
* Defaults to the local time zone
*/
@property(nonatomic, retain) NSTimeZone* localTimeZone;
/**