Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #504 from bmorton/ordered-set

Integrate support for NSOrderedSet
  • Loading branch information...
commit e3264a7bfb98458328a76be41086ce744c16b519 2 parents d5da3ae + 3f0844f
@blakewatters blakewatters authored
View
9 Code/ObjectMapping/RKObjectMappingOperation.m
@@ -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]];
View
38 Specs/ObjectMapping/RKObjectMappingOperationSpec.m
@@ -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
@@ -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"]) {
@@ -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];
Please sign in to comment.
Something went wrong with that request. Please try again.