Add support for mapping to NSData attributes using a NSKeyedArchiver. closes #910

This commit is contained in:
Blake Watters
2012-11-07 11:00:15 -05:00
parent a535ff17be
commit d689ba9291
4 changed files with 23 additions and 35 deletions

View File

@@ -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];

View File

@@ -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

View File

@@ -9,23 +9,7 @@
#import <Foundation/Foundation.h>
#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;

View File

@@ -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];