Permalink
Browse files

Added support for 1-many relationships in POSTed object; improve HTTP…

… DELETE support; fix bug with cleanup of property

RKManagedObject.m
-commented this out as I use string based primary keys
RKClient.m
-calling =nil causes set accessor to execute during cleanup
RKRequest.m
-HTTP DELETE spec expects empty body
RKObjectLoader.m
-modified logging
RKObjectMappable.m
-add support for POST of one-many relationships
RKObjectMapper.m
-modified logging
project.pbxproj
-? xcode generated
  • Loading branch information...
1 parent 976942d commit ff48813fbb4631b30873d3d334cfc71b42979a9c @drsquidop committed Mar 5, 2011
@@ -127,13 +127,14 @@ + (NSString*)primaryKeyElement {
// TODO: Need to inspect the property type here...
+ (id)objectWithPrimaryKeyValue:(id)value {
id primaryKeyValue = nil;
- if ([value isKindOfClass:[NSString class]]) {
- // Cast from string to a number
- primaryKeyValue = [NSNumber numberWithInt:[(NSString*)value integerValue]];
- } else {
+//DWT commented this out as I use string based primary keys
+// if ([value isKindOfClass:[NSString class]]) {
+// // Cast from string to a number
+// primaryKeyValue = [NSNumber numberWithInt:[(NSString*)value integerValue]];
+// } else {
// Make blind assumption here.
primaryKeyValue = value;
- }
+// }
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"%K = %@", [self primaryKeyProperty], primaryKeyValue];
return [self objectWithPredicate:predicate];
}
View
@@ -126,7 +126,8 @@ - (id)init {
}
- (void)dealloc {
- self.baseURL = nil;
+ //DWT calling =nil causes set accessor to execute during cleanup
+ [self.baseURL release];
self.username = nil;
self.password = nil;
self.serviceUnavailableAlertTitle = nil;
View
@@ -111,7 +111,9 @@ - (void)addHeadersToRequest {
// Setup the NSURLRequest. The request must be prepared right before dispatching
- (void)prepareURLRequest {
[_URLRequest setHTTPMethod:[self HTTPMethod]];
- [self setRequestBody];
+ if (_method != RKRequestMethodDELETE) {
+ [self setRequestBody];
+}
[self addHeadersToRequest];
}
@@ -151,7 +151,7 @@ - (void)informDelegateOfObjectLoadWithInfoDictionary:(NSDictionary*)dictionary {
for (id object in models) {
if ([object isKindOfClass:[NSManagedObjectID class]]) {
id obj = [self.managedObjectStore objectWithID:(NSManagedObjectID*)object];
- NSLog(@"OBJ: %@", obj);
+//DWT NSLog(@"OBJ: %@", obj);
[objects addObject:obj];
} else {
[objects addObject:object];
@@ -67,6 +67,8 @@
@end
+NSDictionary *serializeRelationshipsOfModel(NSObject<RKObjectMappable>*object);
+
/**
* Returns a dictionary containing all the mappable properties
* and their values for a given mappable object.
@@ -19,9 +19,58 @@
[propertyNamesAndValues setValue:propertyValue forKey:propertyName];
}
+ //DWT for each relationship mapping, add properties as 'mapping/item'
+
return [NSDictionary dictionaryWithDictionary:propertyNamesAndValues];
}
+// store the keys for one-many relationships
+NSDictionary *serializeRelationshipsOfModel(NSObject<RKObjectMappable>*object) {
+ NSMutableDictionary *elementsAndPropertyValues = [NSMutableDictionary dictionary];
+
+ //NSLog(@"DWT-setRelationshipsOfModel: %@, %@",object,elements);
+ NSDictionary* elementToRelationshipMappings = [[object class] elementToRelationshipMappings];
+ for (NSString* elementKeyPath in elementToRelationshipMappings) {
+ NSString* propertyName = [elementToRelationshipMappings objectForKey:elementKeyPath];
+
+ //for each relationship mapping
+ // create a dict entry for each item in the collection
+ // set the item value to the primaryKey for the related object
+
+ //get the object referred to by propertyName
+ id relationshipElements = nil;
+ @try {
+ relationshipElements = [object valueForKey:propertyName];
+ //for each item in this collection, add an entry with the primary key
+ //if the property is an array, set or dict, findorcreate the related item
+ if ([relationshipElements isKindOfClass:[NSArray class]] || [relationshipElements isKindOfClass:[NSSet class]]) {
+ NSMutableArray *children = [NSMutableArray array];
+ for (id child in relationshipElements) {
+ //get the primary key for this object
+ Class class = [child class];
+ if ([class respondsToSelector:@selector(primaryKeyProperty)]) {
+ NSString* primaryKey = [class performSelector:@selector(primaryKeyProperty)];
+ id primaryKeyValue = [child valueForKey:primaryKey];
+ if (primaryKeyValue != nil) {
+ [children addObject:primaryKeyValue];
+ } else {
+ NSLog(@"ERROR: nil value for primaryKeyProperty for %@. cannot serialize relationship", child);
+ }
+ } else {
+ NSLog(@"ERROR: expected %@ to respond to primaryKeyProperty", child);
+ }
+ }
+ [elementsAndPropertyValues setValue:children forKey:elementKeyPath];
+ }
+ }
+ @catch (NSException* e) {
+ NSLog(@"Caught exception:%@ when trying valueForKey with property:%@ for object:%@", e, propertyName, object);
+ }
+
+ }
+ return elementsAndPropertyValues;
+}
+
// Return all the mapped properties of object in a dictionary under their element names
NSDictionary* RKObjectMappableGetPropertiesByElement(NSObject<RKObjectMappable>*object) {
NSDictionary* mappings = [[object class] elementToPropertyMappings];
@@ -33,5 +82,8 @@
[elementsAndPropertyValues setValue:propertyValue forKey:elementName];
}
+ //serialize the relationships
+ [elementsAndPropertyValues addEntriesFromDictionary:serializeRelationshipsOfModel(object)];
return [NSDictionary dictionaryWithDictionary:elementsAndPropertyValues];
}
+
@@ -196,6 +196,7 @@ - (void)mapObject:(id)model fromDictionary:(NSDictionary*)dictionary {
// TODO: Can I make this support keyPath??
- (id)mapObjectFromDictionary:(NSDictionary*)dictionary {
NSString* elementName = [[dictionary allKeys] objectAtIndex:0];
+ NSLog(@"DWT-mapObjectFromDictionary: %@", elementName);
Class class = [_elementToClassMappings objectForKey:elementName];
NSDictionary* elements = [dictionary objectForKey:elementName];
@@ -353,6 +354,7 @@ - (void)setPropertiesOfModel:(id)model fromElements:(NSDictionary*)elements {
}
- (void)setRelationshipsOfModel:(id)object fromElements:(NSDictionary*)elements {
+ //NSLog(@"DWT-setRelationshipsOfModel: %@, %@",object,elements);
NSDictionary* elementToRelationshipMappings = [[object class] elementToRelationshipMappings];
for (NSString* elementKeyPath in elementToRelationshipMappings) {
NSString* propertyName = [elementToRelationshipMappings objectForKey:elementKeyPath];
@@ -1478,6 +1478,10 @@
projectDirPath = "";
projectReferences = (
{
+ ProductGroup = 25042B7C11F62FF500553519 /* Products */;
+ ProjectRef = 25042A9511F62F2400553519 /* OCMock.xcodeproj */;
+ },
+ {
ProductGroup = 25956591126DE9CF004BAC4C /* Products */;
ProjectRef = 25042C5011F6309000553519 /* OCMock.xcodeproj */;
},
@@ -1486,10 +1490,6 @@
ProjectRef = 250BC43F11F6261700F3FE5A /* OCMock.xcodeproj */;
},
{
- ProductGroup = 25042B7C11F62FF500553519 /* Products */;
- ProjectRef = 25042A9511F62F2400553519 /* OCMock.xcodeproj */;
- },
- {
ProductGroup = 250BC44C11F6262600F3FE5A /* Products */;
ProjectRef = 250BC36211F625DF00F3FE5A /* OCMock.xcodeproj */;
},

0 comments on commit ff48813

Please sign in to comment.