mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-17 23:14:56 +08:00
Merge branch 'unit-testing' into xml-parser
Conflicts: RestKit.xcodeproj/project.pbxproj
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
@implementation RKDynamicRouter
|
||||
|
||||
- (id)init {
|
||||
if (self = [super init]) {
|
||||
if ((self = [super init])) {
|
||||
_routes = [[NSMutableDictionary alloc] init];
|
||||
}
|
||||
|
||||
@@ -80,11 +80,11 @@
|
||||
NSDictionary* classRoutes = [_routes objectForKey:className];
|
||||
|
||||
NSString* resourcePath = nil;
|
||||
if (resourcePath = [classRoutes objectForKey:methodName]) {
|
||||
if ((resourcePath = [classRoutes objectForKey:methodName])) {
|
||||
return RKMakePathWithObject(resourcePath, object);
|
||||
}
|
||||
|
||||
if (resourcePath = [classRoutes objectForKey:@"ANY"]) {
|
||||
if ((resourcePath = [classRoutes objectForKey:@"ANY"])) {
|
||||
return RKMakePathWithObject(resourcePath, object);
|
||||
}
|
||||
|
||||
@@ -94,6 +94,13 @@
|
||||
}
|
||||
|
||||
- (NSObject<RKRequestSerializable>*)serializationForObject:(NSObject<RKObjectMappable>*)object method:(RKRequestMethod)method {
|
||||
// Don't return a serialization for a GET request
|
||||
// There is an extensive discussion about this on the ASIHTTPRequest list
|
||||
// See http://groups.google.com/group/asihttprequest/browse_thread/thread/ef79a8333dde6acb
|
||||
if (method == RKRequestMethodGET) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
// By default return a form encoded serializable dictionary
|
||||
return [object propertiesForSerialization];
|
||||
}
|
||||
|
||||
@@ -57,11 +57,22 @@ typedef enum {
|
||||
*/
|
||||
+ (RKObjectManager*)objectManagerWithBaseURL:(NSString*)baseURL;
|
||||
|
||||
/**
|
||||
* Create and initialize a new object manager. If this is the first instance created
|
||||
* it will be set as the shared instance
|
||||
*/
|
||||
+ (RKObjectManager*)objectManagerWithBaseURL:(NSString*)baseURL objectMapper:(RKObjectMapper*)mapper router:(NSObject<RKRouter>*)router;
|
||||
|
||||
/**
|
||||
* Initialize a new model manager instance
|
||||
*/
|
||||
- (id)initWithBaseURL:(NSString*)baseURL;
|
||||
|
||||
/**
|
||||
* Initialize a new model manager instance
|
||||
*/
|
||||
- (id)initWithBaseURL:(NSString*)baseURL objectMapper:(RKObjectMapper*)mapper router:(NSObject<RKRouter>*)router;
|
||||
|
||||
/**
|
||||
* The wire format to use for communications. Either RKMappingFormatXML or RKMappingFormatJSON.
|
||||
*
|
||||
|
||||
@@ -28,10 +28,15 @@ static RKObjectManager* sharedManager = nil;
|
||||
@synthesize router = _router;
|
||||
|
||||
- (id)initWithBaseURL:(NSString*)baseURL {
|
||||
return self = [self initWithBaseURL:baseURL objectMapper:[[[RKObjectMapper alloc] init] autorelease] router:[[[RKDynamicRouter alloc] init] autorelease]];
|
||||
}
|
||||
|
||||
- (id)initWithBaseURL:(NSString*)baseURL objectMapper:(RKObjectMapper*)mapper router:(NSObject<RKRouter>*)router {
|
||||
if (self = [super init]) {
|
||||
_mapper = [[RKObjectMapper alloc] init];
|
||||
_router = [[RKDynamicRouter alloc] init];
|
||||
_mapper = [mapper retain];
|
||||
_router = [router retain];
|
||||
_client = [[RKClient clientWithBaseURL:baseURL] retain];
|
||||
|
||||
self.format = RKMappingFormatJSON;
|
||||
_onlineState = RKObjectManagerOnlineStateUndetermined;
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
@@ -64,6 +69,14 @@ static RKObjectManager* sharedManager = nil;
|
||||
sharedManager = manager;
|
||||
}
|
||||
|
||||
+ (RKObjectManager*)objectManagerWithBaseURL:(NSString*)baseURL objectMapper:(RKObjectMapper*)mapper router:(NSObject<RKRouter>*)router {
|
||||
RKObjectManager* manager = [[[RKObjectManager alloc] initWithBaseURL:baseURL objectMapper:mapper router:router] autorelease];
|
||||
if (nil == sharedManager) {
|
||||
[RKObjectManager setSharedManager:manager];
|
||||
}
|
||||
return manager;
|
||||
}
|
||||
|
||||
+ (RKObjectManager*)objectManagerWithBaseURL:(NSString*)baseURL {
|
||||
RKObjectManager* manager = [[[RKObjectManager alloc] initWithBaseURL:baseURL] autorelease];
|
||||
if (nil == sharedManager) {
|
||||
|
||||
@@ -27,7 +27,6 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
@interface RKObjectMapper (Private)
|
||||
|
||||
- (id)parseString:(NSString*)string;
|
||||
- (void)updateModel:(id)model fromElements:(NSDictionary*)elements;
|
||||
|
||||
- (Class)typeClassForProperty:(NSString*)property ofClass:(Class)class;
|
||||
- (NSDictionary*)elementToPropertyMappingsForModel:(id)model;
|
||||
@@ -36,9 +35,9 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
- (id)createOrUpdateInstanceOfModelClass:(Class)class fromElements:(NSDictionary*)elements;
|
||||
|
||||
- (void)updateModel:(id)model ifNewPropertyValue:(id)propertyValue forPropertyNamed:(NSString*)propertyName; // Rename!
|
||||
- (void)updateModel:(id)model fromElements:(NSDictionary*)elements;
|
||||
- (void)setPropertiesOfModel:(id)model fromElements:(NSDictionary*)elements;
|
||||
- (void)setRelationshipsOfModel:(id)object fromElements:(NSDictionary*)elements;
|
||||
- (void)updateModel:(id)model fromElements:(NSDictionary*)elements;
|
||||
|
||||
- (NSDate*)parseDateFromString:(NSString*)string;
|
||||
- (NSDate*)dateInLocalTime:(NSDate*)date;
|
||||
@@ -401,14 +400,22 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
@catch (NSException* e) {
|
||||
NSLog(@"Caught exception:%@ when trying valueForKeyPath with path:%@ for elements:%@", e, elementKeyPath, elements);
|
||||
}
|
||||
|
||||
// TODO: Need to send NSSet or NSArray depending on what the property type is...
|
||||
|
||||
// NOTE: The last part of the keyPath contains the elementName for the mapped destination class of our children
|
||||
NSArray* componentsOfKeyPath = [elementKeyPath componentsSeparatedByString:@"."];
|
||||
NSString *className = [componentsOfKeyPath objectAtIndex:[componentsOfKeyPath count] - 1];
|
||||
Class modelClass = [_elementToClassMappings objectForKey:className];
|
||||
if ([modelClass isKindOfClass: [NSNull class]]) {
|
||||
NSLog(@"Warning: could not find a class mapping for relationship '%@':", className);
|
||||
NSLog(@" parent class : %@", [object class]);
|
||||
NSLog(@" elements to map: %@", elements);
|
||||
NSLog(@"maybe you want to register your model with the object mapper or you want to pluralize the keypath?");
|
||||
break;
|
||||
}
|
||||
|
||||
Class collectionClass = [self typeClassForProperty:propertyName ofClass:[object class]];
|
||||
// if ([relationshipElements isKindOfClass:[NSArray class]] || [relationshipElements isKindOfClass:[NSSet class]]) {
|
||||
if ([collectionClass isSubclassOfClass:[NSSet class]] || [collectionClass isSubclassOfClass:[NSArray class]]) {
|
||||
// NOTE: The last part of the keyPath contains the elementName for the mapped destination class of our children
|
||||
NSArray* componentsOfKeyPath = [elementKeyPath componentsSeparatedByString:@"."];
|
||||
Class class = [_elementToClassMappings objectForKey:[componentsOfKeyPath objectAtIndex:[componentsOfKeyPath count] - 1]];
|
||||
if ([collectionClass isSubclassOfClass:[NSSet class]] || [collectionClass isSubclassOfClass:[NSArray class]])
|
||||
{
|
||||
id children = nil;
|
||||
if ([collectionClass isSubclassOfClass:[NSSet class]]) {
|
||||
children = [NSMutableSet setWithCapacity:[relationshipElements count]];
|
||||
@@ -416,45 +423,44 @@ static const NSString* kRKModelMapperXMLMappingFormatParserKey = @"RKXMLMappingF
|
||||
children = [NSMutableArray arrayWithCapacity:[relationshipElements count]];
|
||||
}
|
||||
|
||||
for (NSDictionary* childElements in relationshipElements) {
|
||||
id child = [self createOrUpdateInstanceOfModelClass:class fromElements:childElements];
|
||||
if (child) {
|
||||
[(NSMutableArray*)children addObject:child];
|
||||
}
|
||||
}
|
||||
|
||||
[object setValue:children forKey:propertyName];
|
||||
} else if ([relationshipElements isKindOfClass:[NSDictionary class]]) {
|
||||
NSArray* componentsOfKeyPath = [elementKeyPath componentsSeparatedByString:@"."];
|
||||
Class class = [_elementToClassMappings objectForKey:[componentsOfKeyPath objectAtIndex:[componentsOfKeyPath count] - 1]];
|
||||
id child = [self createOrUpdateInstanceOfModelClass:class fromElements:relationshipElements];
|
||||
[object setValue:child forKey:propertyName];
|
||||
}
|
||||
}
|
||||
|
||||
if ([object isKindOfClass:[RKManagedObject class]]) {
|
||||
RKManagedObject* managedObject = (RKManagedObject*)object;
|
||||
NSDictionary* relationshipToPkPropertyMappings = [[managedObject class] relationshipToPrimaryKeyPropertyMappings];
|
||||
for (NSString* relationship in relationshipToPkPropertyMappings) {
|
||||
NSString* primaryKeyPropertyString = [relationshipToPkPropertyMappings objectForKey:relationship];
|
||||
|
||||
NSNumber* objectPrimaryKeyValue = nil;
|
||||
@try {
|
||||
objectPrimaryKeyValue = [managedObject valueForKeyPath:primaryKeyPropertyString];
|
||||
} @catch (NSException* e) {
|
||||
NSLog(@"Caught exception:%@ when trying valueForKeyPath with path:%@ for object:%@", e, primaryKeyPropertyString, managedObject);
|
||||
}
|
||||
|
||||
NSDictionary* relationshipsByName = [[managedObject entity] relationshipsByName];
|
||||
NSEntityDescription* relationshipDestinationEntity = [[relationshipsByName objectForKey:relationship] destinationEntity];
|
||||
id relationshipDestinationClass = objc_getClass([[relationshipDestinationEntity managedObjectClassName] cStringUsingEncoding:NSUTF8StringEncoding]);
|
||||
RKManagedObject* relationshipValue = [[[RKObjectManager sharedManager] objectStore] findOrCreateInstanceOfManagedObject:relationshipDestinationClass
|
||||
for (NSDictionary* childElements in relationshipElements) {
|
||||
id child = [self createOrUpdateInstanceOfModelClass:modelClass fromElements:childElements];
|
||||
if (child) {
|
||||
[(NSMutableArray*)children addObject:child];
|
||||
}
|
||||
}
|
||||
|
||||
[object setValue:children forKey:propertyName];
|
||||
} else if ([relationshipElements isKindOfClass:[NSDictionary class]]) {
|
||||
id child = [self createOrUpdateInstanceOfModelClass:modelClass fromElements:relationshipElements];
|
||||
[object setValue:child forKey:propertyName];
|
||||
}
|
||||
|
||||
}
|
||||
if ([object isKindOfClass:[RKManagedObject class]]) {
|
||||
RKManagedObject* managedObject = (RKManagedObject*)object;
|
||||
NSDictionary* relationshipToPkPropertyMappings = [[managedObject class] relationshipToPrimaryKeyPropertyMappings];
|
||||
for (NSString* relationship in relationshipToPkPropertyMappings) {
|
||||
NSString* primaryKeyPropertyString = [relationshipToPkPropertyMappings objectForKey:relationship];
|
||||
|
||||
NSNumber* objectPrimaryKeyValue = nil;
|
||||
@try {
|
||||
objectPrimaryKeyValue = [managedObject valueForKeyPath:primaryKeyPropertyString];
|
||||
} @catch (NSException* e) {
|
||||
NSLog(@"Caught exception:%@ when trying valueForKeyPath with path:%@ for object:%@", e, primaryKeyPropertyString, managedObject);
|
||||
}
|
||||
|
||||
NSDictionary* relationshipsByName = [[managedObject entity] relationshipsByName];
|
||||
NSEntityDescription* relationshipDestinationEntity = [[relationshipsByName objectForKey:relationship] destinationEntity];
|
||||
id relationshipDestinationClass = objc_getClass([[relationshipDestinationEntity managedObjectClassName] cStringUsingEncoding:NSUTF8StringEncoding]);
|
||||
RKManagedObject* relationshipValue = [[[RKObjectManager sharedManager] objectStore] findOrCreateInstanceOfManagedObject:relationshipDestinationClass
|
||||
withPrimaryKeyValue:objectPrimaryKeyValue];
|
||||
if (relationshipValue) {
|
||||
[managedObject setValue:relationshipValue forKey:relationship];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (relationshipValue) {
|
||||
[managedObject setValue:relationshipValue forKey:relationship];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
- (void)updateModel:(id)model fromElements:(NSDictionary*)elements {
|
||||
|
||||
Reference in New Issue
Block a user