mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-01-12 22:51:50 +08:00
Enable metadata mapping for identification attributes. fixes #1202
This commit is contained in:
@@ -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];
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user