RKObjectMappingOperation#parseDateFromString now transforms string representations of integers into dates as if they were unix timestamps

This commit is contained in:
Christopher Swasey
2012-01-20 12:59:08 -05:00
parent 37f40d0d3a
commit b1f4e1ffe5
2 changed files with 33 additions and 6 deletions

View File

@@ -113,14 +113,27 @@ BOOL RKObjectIsValueEqualToValue(id sourceValue, id destinationValue) {
- (NSDate*)parseDateFromString:(NSString*)string {
RKLogTrace(@"Transforming string value '%@' to NSDate...", string);
NSDate* date = nil;
for (NSDateFormatter *dateFormatter in self.objectMapping.dateFormatters) {
@synchronized(dateFormatter) {
NSDate* date = nil;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *numeric = [numberFormatter numberFromString:string];
[numberFormatter release];
if (numeric) {
date = [NSDate dateWithTimeIntervalSince1970:[numeric doubleValue]];
} else {
for (NSDateFormatter *dateFormatter in self.objectMapping.dateFormatters) {
@synchronized(dateFormatter) {
date = [dateFormatter dateFromString:string];
}
if (date) {
break;
}
}
if (date) {
break;
}
}
return date;

View File

@@ -222,6 +222,20 @@
[operation release];
}
- (void)testShouldMapAUnixTimestampStringAppropriately {
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[TestMappable class]];
[mapping mapAttributes:@"date", nil];
TestMappable* object = [[[TestMappable alloc] init] autorelease];
NSDictionary* dictionary = [NSDictionary dictionaryWithObject:@"457574400" forKey:@"date"];
RKObjectMappingOperation* operation = [[RKObjectMappingOperation alloc] initWithSourceObject:dictionary destinationObject:object mapping:mapping];
NSError* error = nil;
BOOL success = [operation performMapping:&error];
assertThatBool(success, is(equalToBool(YES)));
assertThat(object.date, isNot(nilValue()));
assertThat([object.date description], is(equalTo(@"1984-07-02 00:00:00 +0000")));
[operation release];
}
- (void)testShouldMapASimpleDateStringAppropriately {
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[TestMappable class]];
[mapping mapAttributes:@"date", nil];