Enable metadata mapping for identification attributes. fixes #1202

This commit is contained in:
Blake Watters
2013-01-31 11:47:41 -05:00
parent ec180fae5b
commit 32c52f2d06
3 changed files with 35 additions and 1 deletions

View File

@@ -149,7 +149,10 @@ static BOOL entityIdentificationInferenceEnabled = YES;
+ (instancetype)mappingForEntityForName:(NSString *)entityName inManagedObjectStore:(RKManagedObjectStore *)managedObjectStore
{
NSParameterAssert(entityName);
NSParameterAssert(managedObjectStore);
NSEntityDescription *entity = [[managedObjectStore.managedObjectModel entitiesByName] objectForKey:entityName];
NSAssert(entity, @"Unable to find an Entity with the name '%@' in the managed object model", entityName);
return [[self alloc] initWithEntity:entity];
}

View File

@@ -50,6 +50,11 @@ static NSString *RKFailureReasonErrorStringForMappingNotFoundError(id representa
return failureReason;
}
// Duplicating interface from `RKMappingOperation.m`
@interface RKMappingSourceObject : NSProxy
- (id)initWithObject:(id)object metadata:(NSDictionary *)metadata;
@end
@interface RKMapperOperation ()
@property (nonatomic, strong, readwrite) NSError *error;
@@ -287,7 +292,8 @@ static NSString *RKFailureReasonErrorStringForMappingNotFoundError(id representa
}
if (objectMapping) {
return [self.mappingOperationDataSource mappingOperation:nil targetObjectForRepresentation:representation withMapping:objectMapping inRelationship:nil];
id mappingSourceObject = [[RKMappingSourceObject alloc] initWithObject:representation metadata:self.metadata];
return [self.mappingOperationDataSource mappingOperation:nil targetObjectForRepresentation:mappingSourceObject withMapping:objectMapping inRelationship:nil];
}
return nil;

View File

@@ -1250,4 +1250,29 @@
expect(mappingResult).willNot.beNil();
}
- (void)testUseOfMetadataMappingAsIdentificationAttribute
{
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[RKTestFactory baseURL]];
objectManager.managedObjectStore = [RKTestFactory managedObjectStore];
RKEntityMapping *humanMapping = [RKEntityMapping mappingForEntityForName:@"Human" inManagedObjectStore:objectManager.managedObjectStore];
[humanMapping addAttributeMappingsFromDictionary:@{ @"name": @"name", @"@metadata.routing.parameters.railsID": @"favoriteCatID" }];
[humanMapping setIdentificationAttributes:@[ @"favoriteCatID" ]];
[objectManager.router.routeSet addRoute:[RKRoute routeWithName:@"load_human" pathPattern:@"/JSON/humans/:railsID\\.json" method:RKRequestMethodGET]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:humanMapping pathPattern:@"/JSON/humans/:railsID\\.json" keyPath:@"human" statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
RKHuman *human = [NSEntityDescription insertNewObjectForEntityForName:@"Human" inManagedObjectContext:objectManager.managedObjectStore.mainQueueManagedObjectContext];
human.railsID = @1;
__block RKMappingResult *mappingResult = nil;
[objectManager getObjectsAtPathForRouteNamed:@"load_human" object:human parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *blockMappingResult) {
mappingResult = blockMappingResult;
} failure:nil];
expect(mappingResult).willNot.beNil();
RKHuman *anotherHuman = [mappingResult firstObject];
expect(anotherHuman.name).to.equal(@"Blake Watters");
expect(anotherHuman.favoriteCatID).to.equal(@1);
expect([anotherHuman isEqual:human]).to.beFalsy();
}
@end