From d689ba92910c0ea65a5562c98dcdc2b975993523 Mon Sep 17 00:00:00 2001 From: Blake Watters Date: Wed, 7 Nov 2012 11:00:15 -0500 Subject: [PATCH] Add support for mapping to NSData attributes using a `NSKeyedArchiver`. closes #910 --- Code/ObjectMapping/RKMappingOperation.m | 4 +++- .../RKObjectMappingNextGenTest.m | 18 ++++++++++++++++++ Tests/Models/RKTestUser.h | 19 ++----------------- Tests/Models/RKTestUser.m | 17 ----------------- 4 files changed, 23 insertions(+), 35 deletions(-) diff --git a/Code/ObjectMapping/RKMappingOperation.m b/Code/ObjectMapping/RKMappingOperation.m index c6029802..9723f891 100644 --- a/Code/ObjectMapping/RKMappingOperation.m +++ b/Code/ObjectMapping/RKMappingOperation.m @@ -115,7 +115,9 @@ static BOOL RKIsManagedObject(id object) RKLogTrace(@"Found transformable value at keyPath '%@'. Transforming from type '%@' to '%@'", keyPath, NSStringFromClass([value class]), NSStringFromClass(destinationType)); Class sourceType = [value class]; - if ([sourceType isSubclassOfClass:[NSString class]]) { + if ([destinationType isSubclassOfClass:[NSData class]]) { + return [NSKeyedArchiver archivedDataWithRootObject:value]; + } else if ([sourceType isSubclassOfClass:[NSString class]]) { if ([destinationType isSubclassOfClass:[NSDate class]]) { // String -> Date return [self parseDateFromString:(NSString *)value]; diff --git a/Tests/Logic/ObjectMapping/RKObjectMappingNextGenTest.m b/Tests/Logic/ObjectMapping/RKObjectMappingNextGenTest.m index a361a370..a913eb90 100644 --- a/Tests/Logic/ObjectMapping/RKObjectMappingNextGenTest.m +++ b/Tests/Logic/ObjectMapping/RKObjectMappingNextGenTest.m @@ -1269,6 +1269,24 @@ assertThat(user.name, is(equalTo(@"Blake Watters"))); } +- (void)testMappingToAnNSDataAttributeUsingKeyedArchiver +{ + RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[RKTestUser class]]; + RKAttributeMapping *attributeMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"arrayOfStrings" toKeyPath:@"data"]; + [mapping addPropertyMapping:attributeMapping]; + + NSDictionary *dictionary = @{ @"arrayOfStrings": @[ @"one", @"two", @"three" ] }; + RKTestUser *user = [RKTestUser user]; + RKMappingOperation *operation = [[RKMappingOperation alloc] initWithSourceObject:dictionary destinationObject:user mapping:mapping]; + NSError *error = nil; + BOOL success = [operation performMapping:&error]; + expect(success).to.equal(YES); + expect(user.data).notTo.beNil(); + NSDictionary *decodedDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:user.data]; + NSArray *expectedValue = @[ @"one", @"two", @"three" ]; + expect(decodedDictionary).to.equal(expectedValue); +} + #pragma mark - Relationship Mapping - (void)testShouldMapANestedObject diff --git a/Tests/Models/RKTestUser.h b/Tests/Models/RKTestUser.h index a7b07f36..bb90c76e 100644 --- a/Tests/Models/RKTestUser.h +++ b/Tests/Models/RKTestUser.h @@ -9,23 +9,7 @@ #import #import "RKTestAddress.h" -@interface RKTestUser : NSObject { - NSNumber *_userID; - NSString *_name; - NSDate *_birthDate; - NSArray *_favoriteColors; - NSDictionary *_addressDictionary; - NSURL *_website; - NSNumber *_isDeveloper; - NSNumber *_luckyNumber; - NSDecimalNumber *_weight; - NSArray *_interests; - NSString *_country; - - // Relationships - RKTestAddress *_address; - NSArray *_friends; -} +@interface RKTestUser : NSObject @property (nonatomic, strong) NSNumber *userID; @property (nonatomic, strong) NSString *name; @@ -43,6 +27,7 @@ @property (nonatomic, strong) NSArray *friends; @property (nonatomic, strong) NSSet *friendsSet; @property (nonatomic, strong) NSOrderedSet *friendsOrderedSet; +@property (nonatomic, strong) NSData *data; + (RKTestUser *)user; diff --git a/Tests/Models/RKTestUser.m b/Tests/Models/RKTestUser.m index f01178c9..38656abd 100644 --- a/Tests/Models/RKTestUser.m +++ b/Tests/Models/RKTestUser.m @@ -11,23 +11,6 @@ @implementation RKTestUser -@synthesize userID = _userID; -@synthesize name = _name; -@synthesize birthDate = _birthDate; -@synthesize favoriteDate = _favoriteDate; -@synthesize favoriteColors = _favoriteColors; -@synthesize addressDictionary = _addressDictionary; -@synthesize website = _website; -@synthesize isDeveloper = _isDeveloper; -@synthesize luckyNumber = _luckyNumber; -@synthesize weight = _weight; -@synthesize interests = _interests; -@synthesize country = _country; -@synthesize address = _address; -@synthesize friends = _friends; -@synthesize friendsSet = _friendsSet; -@synthesize friendsOrderedSet = _friendsOrderedSet; - + (RKTestUser *)user { return [self new];