Added support for mapping boolean strings to NSNumber with values t or f in addition to true and false. refs #130

This commit is contained in:
Blake Watters
2011-06-08 19:07:56 -04:00
parent 8be34582a5
commit 7bf3e0f273
2 changed files with 35 additions and 2 deletions

View File

@@ -85,9 +85,11 @@
} else if ([destinationType isSubclassOfClass:[NSNumber class]]) {
// String -> Number
NSString* lowercasedString = [(NSString*)value lowercaseString];
if ([lowercasedString isEqualToString:@"true"] || [lowercasedString isEqualToString:@"false"]) {
NSSet* trueStrings = [NSSet setWithObjects:@"true", @"t", nil];
NSSet* booleanStrings = [trueStrings setByAddingObjectsFromSet:[NSSet setWithObjects:@"false", @"f", nil]];
if ([booleanStrings containsObject:lowercasedString]) {
// Handle booleans encoded as Strings
return [NSNumber numberWithBool:[lowercasedString isEqualToString:@"true"]];
return [NSNumber numberWithBool:[trueStrings containsObject:lowercasedString]];
} else {
return [NSNumber numberWithDouble:[(NSString*)value doubleValue]];
}

View File

@@ -746,6 +746,37 @@
[expectThat([[user isDeveloper] boolValue]) should:be(YES)];
}
- (void)itShouldMapAShortTrueStringToANumberBool {
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RKExampleUser class]];
RKObjectAttributeMapping* websiteMapping = [RKObjectAttributeMapping mappingFromKeyPath:@"is_developer" toKeyPath:@"isDeveloper"];
[mapping addAttributeMapping:websiteMapping];
NSDictionary* dictionary = [RKSpecParseFixtureJSON(@"user.json") mutableCopy];
RKExampleUser* user = [RKExampleUser user];
[dictionary setValue:@"T" forKey:@"is_developer"];
RKObjectMappingOperation* operation = [[RKObjectMappingOperation alloc] initWithSourceObject:dictionary destinationObject:user objectMapping:mapping];
NSError* error = nil;
[operation performMapping:&error];
[expectThat([[user isDeveloper] boolValue]) should:be(YES)];
}
- (void)itShouldMapAShortFalseStringToANumberBool {
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RKExampleUser class]];
RKObjectAttributeMapping* websiteMapping = [RKObjectAttributeMapping mappingFromKeyPath:@"is_developer" toKeyPath:@"isDeveloper"];
[mapping addAttributeMapping:websiteMapping];
NSDictionary* dictionary = [RKSpecParseFixtureJSON(@"user.json") mutableCopy];
RKExampleUser* user = [RKExampleUser user];
[dictionary setValue:@"f" forKey:@"is_developer"];
RKObjectMappingOperation* operation = [[RKObjectMappingOperation alloc] initWithSourceObject:dictionary destinationObject:user objectMapping:mapping];
NSError* error = nil;
[operation performMapping:&error];
[expectThat([[user isDeveloper] boolValue]) should:be(NO)];
}
- (void)itShouldMapAStringToANumber {
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RKExampleUser class]];
RKObjectAttributeMapping* websiteMapping = [RKObjectAttributeMapping mappingFromKeyPath:@"lucky_number" toKeyPath:@"luckyNumber"];