Skip to content

Commit

Permalink
Merge pull request RestKit#504 from bmorton/ordered-set
Browse files Browse the repository at this point in the history
Integrate support for NSOrderedSet
  • Loading branch information
blakewatters committed Jan 14, 2012
2 parents d5da3ae + 3f0844f commit e3264a7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Code/ObjectMapping/RKObjectMappingOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,20 @@ - (id)transformValue:(id)value atKeyPath:keyPath toType:(Class)destinationType {
if ([destinationType isSubclassOfClass:[NSArray class]]) {
return [(NSSet*)value allObjects];
}
} else if ([sourceType isSubclassOfClass:[NSOrderedSet class]]) {
// OrderedSet -> Array
if ([destinationType isSubclassOfClass:[NSArray class]]) {
return [(NSOrderedSet*)value array];
}
} else if ([sourceType isSubclassOfClass:[NSArray class]]) {
// Array -> Set
if ([destinationType isSubclassOfClass:[NSSet class]]) {
return [NSSet setWithArray:value];
}
// Array -> OrderedSet
if ([destinationType isSubclassOfClass:[NSOrderedSet class]]) {
return [NSOrderedSet orderedSetWithArray:value];
}
} else if ([sourceType isSubclassOfClass:[NSNumber class]] && [destinationType isSubclassOfClass:[NSDate class]]) {
// Number -> Date
return [NSDate dateWithTimeIntervalSince1970:[(NSNumber*)value intValue]];
Expand Down
38 changes: 38 additions & 0 deletions Specs/ObjectMapping/RKObjectMappingOperationSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ @interface TestMappable : NSObject {
NSURL *_url;
NSString *_boolString;
NSDate *_date;
NSOrderedSet *_orderedSet;
NSArray *_array;
}

@property (nonatomic, retain) NSURL *url;
@property (nonatomic, retain) NSString *boolString;
@property (nonatomic, retain) NSNumber *boolNumber;
@property (nonatomic, retain) NSDate *date;
@property (nonatomic, retain) NSOrderedSet *orderedSet;
@property (nonatomic, retain) NSArray *array;

@end

Expand All @@ -42,6 +46,8 @@ @implementation TestMappable
@synthesize boolString = _boolString;
@synthesize boolNumber = _boolNumber;
@synthesize date = _date;
@synthesize orderedSet = _orderedSet;
@synthesize array = _array;

- (BOOL)validateBoolString:(id *)ioValue error:(NSError **)outError {
if ([(NSObject *)*ioValue isKindOfClass:[NSString class]] && [(NSString *)*ioValue isEqualToString:@"FAIL"]) {
Expand Down Expand Up @@ -141,6 +147,38 @@ - (void)testShouldSuccessfullyMapNumbersToStrings {
[operation release];
}

- (void)testShouldSuccessfullyMapArraysToOrderedSets {
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[TestMappable class]];
[mapping mapKeyPath:@"numbers" toAttribute:@"orderedSet"];
TestMappable* object = [[[TestMappable alloc] init] autorelease];

id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:@"application/json"];
id data = [parser objectFromString:@"{\"numbers\":[1, 2, 3]}" error:nil];

RKObjectMappingOperation* operation = [[RKObjectMappingOperation alloc] initWithSourceObject:data destinationObject:object mapping:mapping];
BOOL success = [operation performMapping:nil];
assertThatBool(success, is(equalToBool(YES)));
NSOrderedSet *expectedSet = [NSOrderedSet orderedSetWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil];
assertThat(object.orderedSet, is(equalTo(expectedSet)));
[operation release];
}

- (void)testShouldSuccessfullyMapOrderedSetsToArrays {
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[TestMappable class]];
[mapping mapKeyPath:@"orderedSet" toAttribute:@"array"];
TestMappable* object = [[[TestMappable alloc] init] autorelease];

TestMappable* data = [[[TestMappable alloc] init] autorelease];
data.orderedSet = [NSOrderedSet orderedSetWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil];

RKObjectMappingOperation* operation = [[RKObjectMappingOperation alloc] initWithSourceObject:data destinationObject:object mapping:mapping];
BOOL success = [operation performMapping:nil];
assertThatBool(success, is(equalToBool(YES)));
NSArray* expectedArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil];
assertThat(object.array, is(equalTo(expectedArray)));
[operation release];
}

- (void)testShouldFailTheMappingOperationIfKeyValueValidationSetsAnError {
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[TestMappable class]];
[mapping mapAttributes:@"boolString", nil];
Expand Down

0 comments on commit e3264a7

Please sign in to comment.