Skip to content

Commit

Permalink
Handle comma-separated string for RKEntityIdentificationAttributes
Browse files Browse the repository at this point in the history
This is useful if you want to enter the identification attributes directly in the xcdatamodel editor where the only type available for the User Info dictionary is string.
  • Loading branch information
0xced committed Sep 20, 2015
1 parent 9cb579c commit b2f5c2d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Code/CoreData/RKEntityMapping.m
Expand Up @@ -39,7 +39,15 @@
do {
id userInfoValue = [[entity userInfo] valueForKey:RKEntityIdentificationAttributesUserInfoKey];
if (userInfoValue) {
NSArray *attributeNames = [userInfoValue isKindOfClass:[NSArray class]] ? userInfoValue : @[ userInfoValue ];
NSArray *attributeNames;
if ([userInfoValue isKindOfClass:[NSString class]]) {
attributeNames = [userInfoValue componentsSeparatedByString:@","];
} else if ([userInfoValue isKindOfClass:[NSArray class]]) {
attributeNames = userInfoValue;
} else {
attributeNames = @[ userInfoValue ];
}

NSMutableArray *attributes = [NSMutableArray arrayWithCapacity:[attributeNames count]];
[attributeNames enumerateObjectsUsingBlock:^(NSString *attributeName, NSUInteger idx, BOOL *stop) {
if (! [attributeName isKindOfClass:[NSString class]]) {
Expand Down
16 changes: 16 additions & 0 deletions Tests/Logic/CoreData/RKEntityMappingTest.m
Expand Up @@ -619,6 +619,22 @@ - (void)testEntityIdentifierInferenceFromUserInfoKeyForSingleValue
expect([identificationAttributes valueForKey:@"name"]).to.equal(attributeNames);
}

- (void)testEntityIdentifierInferenceFromUserInfoKeyForCommaSeparatedString
{
NSEntityDescription *entity = [[NSEntityDescription alloc] init];
[entity setName:@"Monkey"];
NSAttributeDescription *identifierAttribute = [NSAttributeDescription new];
[identifierAttribute setName:@"monkeyID"];
NSAttributeDescription *nameAttribute = [NSAttributeDescription new];
[nameAttribute setName:@"name"];
[entity setProperties:@[ identifierAttribute, nameAttribute ]];
[entity setUserInfo:@{ RKEntityIdentificationAttributesUserInfoKey: @"name,monkeyID" }];
NSArray *identificationAttributes = RKIdentificationAttributesInferredFromEntity(entity);
expect(identificationAttributes).notTo.beNil();
NSArray *attributeNames = @[ @"name", @"monkeyID" ];
expect([identificationAttributes valueForKey:@"name"]).to.equal(attributeNames);
}

- (void)testEntityIdentifierInferenceFromUserInfoKeyForArrayOfValues
{
NSEntityDescription *entity = [[NSEntityDescription alloc] init];
Expand Down

0 comments on commit b2f5c2d

Please sign in to comment.