Skip to content
Browse files

Add NSURL support

  • Loading branch information...
1 parent d36ad46 commit 73f0a0e8bee34a0d7bcd9390d3b06a590790516e @TheSantaClaus TheSantaClaus committed Nov 14, 2013
View
6 JTObjectMapping.xcodeproj/project.pbxproj
@@ -33,6 +33,7 @@
1A7E720B141605B300AE157A /* JTMappings.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A7E720A141605B300AE157A /* JTMappings.m */; };
1A7E720E141613C900AE157A /* JTUserTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A7E720D141613C800AE157A /* JTUserTest.m */; };
1AF05842141D1C9A00CD7E50 /* JTObjectMappingArrayTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AF05841141D1C9A00CD7E50 /* JTObjectMappingArrayTest.m */; };
+ 681D24CF1835327A00200A11 /* JTURLMappings.m in Sources */ = {isa = PBXBuildFile; fileRef = 681D24CE1835327A00200A11 /* JTURLMappings.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -86,6 +87,8 @@
1A7E720D141613C800AE157A /* JTUserTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JTUserTest.m; sourceTree = "<group>"; };
1AF05840141D1C9A00CD7E50 /* JTObjectMappingArrayTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JTObjectMappingArrayTest.h; sourceTree = "<group>"; };
1AF05841141D1C9A00CD7E50 /* JTObjectMappingArrayTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JTObjectMappingArrayTest.m; sourceTree = "<group>"; };
+ 681D24CD1835327A00200A11 /* JTURLMappings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JTURLMappings.h; sourceTree = "<group>"; };
+ 681D24CE1835327A00200A11 /* JTURLMappings.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JTURLMappings.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -211,6 +214,8 @@
1197E87C15C6F79C006BC9F0 /* JTSetMappings.m */,
1A2503E115F26AE500490DAD /* JTValidMappingKey.h */,
1A2503E215F26AE500490DAD /* JTValidMappingKey.m */,
+ 681D24CD1835327A00200A11 /* JTURLMappings.h */,
+ 681D24CE1835327A00200A11 /* JTURLMappings.m */,
);
path = Source;
sourceTree = "<group>";
@@ -327,6 +332,7 @@
1A7E71D61415F96800AE157A /* JTObjectMappingAppDelegate.m in Sources */,
1A7E71DC1415F96800AE157A /* JTObjectMappingViewController.m in Sources */,
1A7E72021415FA1500AE157A /* NSObject+JTObjectMapping.m in Sources */,
+ 681D24CF1835327A00200A11 /* JTURLMappings.m in Sources */,
1A7E7208141602C900AE157A /* JTSocialNetworkTest.m in Sources */,
1A7E720B141605B300AE157A /* JTMappings.m in Sources */,
1A7E720E141613C900AE157A /* JTUserTest.m in Sources */,
View
16 JTObjectMapping/Source/JTURLMappings.h
@@ -0,0 +1,16 @@
+//
+// NSURL+JTURLMapping.h
+// JTObjectMapping
+//
+// Created by Vladimir Shevchenko on 11/14/13.
+//
+//
+
+#import <Foundation/Foundation.h>
+#import "JTValidMappingKey.h"
+
+@interface NSURL (JTValidMappingKey)
+
++ (id <JTValidMappingKey>)mappingWithKey:(NSString *)key;
+
+@end
View
63 JTObjectMapping/Source/JTURLMappings.m
@@ -0,0 +1,63 @@
+//
+// NSURL+JTURLMapping.m
+// JTObjectMapping
+//
+// Created by Vladimir Shevchenko on 11/14/13.
+//
+//
+
+#import "JTURLMappings.h"
+
+@interface JTURLMappings : NSObject <JTValidMappingKey>
+
+@property (nonatomic, copy) NSString *key;
+
+@end
+
+@implementation JTURLMappings
+
++ (id <JTValidMappingKey>)mappingWithKey:(NSString *)key
+{
+ JTURLMappings *map = [[JTURLMappings alloc] init];
+ map.key = key;
+ return [map autorelease];
+}
+
+- (void)dealloc {
+ self.key = nil;
+ [super dealloc];
+}
+
+- (BOOL)transformValue:(NSObject *)oldValue
+ toValue:(NSObject **)newValue
+ forKey:(NSString **)key {
+
+ if ([oldValue isKindOfClass:[NSString class]]) {
+
+ NSURL *url = [NSURL URLWithString:(NSString *)oldValue];
+ *newValue = url;
+ *key = self.key;
+
+ return (url != nil);
+
+ } else if ([oldValue isKindOfClass:[NSNull class]]) {
+
+ *newValue = nil;
+ *key = self.key;
+
+ return YES;
+ }
+
+ return NO;
+}
+
+@end
+
+@implementation NSURL (JTURLMappings)
+
++ (id <JTValidMappingKey>)mappingWithKey:(NSString *)key
+{
+ return [JTURLMappings mappingWithKey:key];
+}
+
+@end
View
3 JTObjectMapping/Source/NSObject+JTObjectMapping.h
@@ -11,6 +11,7 @@
#import "JTDateMappings.h"
#import "JTMappings.h"
#import "JTSetMappings.h"
+#import "JTURLMappings.h"
@protocol JTValidJSONResponse <NSObject>
@end
@@ -21,6 +22,8 @@
@interface NSDictionary () <JTValidJSONResponse>
@end
+@interface NSURL () <JTValidJSONResponse>
+@end
@interface NSObject (JTObjectMapping)
View
12 JTObjectMappingTests/JTObjectMappingTests.m
@@ -17,7 +17,7 @@
// when the unicode string is mapped with lossy ASCII the elipses character (0x2026) will convert to three periods
#define DATA_STRING_UNICODE @"elipses are unicode characters…periods are not"
#define DATA_STRING_ASCII @"elipses are unicode characters...periods are not"
-
+#define AVATAR_URL @"http://en.gravatar.com/userimage/11332249/d73901242ae1c7e33bcc7c83257ac165.jpg"
@implementation JTObjectMappingTests
@synthesize json, mapping, object;
@@ -65,6 +65,8 @@ - (void)setUp
// NSData -- the mapping below will specify lossy ascii
DATA_STRING_UNICODE, @"dataLossy",
+ AVATAR_URL, @"avatarURL",
+
// auto string
@"yes", @"autoString",
[NSArray arrayWithObjects:
@@ -151,6 +153,8 @@ - (void)setUp
// NSData mapping (lossy ascii)
[NSData mappingWithKey:@"dataLossy" usingEncoding:NSASCIIStringEncoding allowLossy:YES], @"dataLossy",
+ [NSURL mappingWithKey:@"avatarURL"], @"avatarURL",
+
// This specifies a mapping a child object (JTSocialNetwork) and a child dictionary in the json dictionary
// (it too uses a map of json keys to its properties, the `socialNetworkMapping` dictionary)
[JTSocialNetworkTest mappingWithKey:@"socialNetwork"
@@ -268,6 +272,12 @@ - (void)testSet {
STAssertTrue([colors containsObject:@"blue"], @"%@ should contain 'blue'", colors);
}
+- (void)testURL {
+ NSURL *url = self.object.avatarURL;
+ STAssertTrue([url isKindOfClass:[NSURL class]], @"%@ != [NSURL class]", [url class]);
+ STAssertTrue([url.absoluteString isEqualToString:AVATAR_URL], @"%@ != %@", url.absoluteString, AVATAR_URL);
+}
+
- (void)testKeyPath {
STAssertEqualObjects(self.object.hashedString, @"string", nil, nil);
View
1 JTObjectMappingTests/JTUserTest.h
@@ -22,6 +22,7 @@
@property (nonatomic, retain) NSData *data;
@property (nonatomic, retain) NSData *dataLossy;
@property (nonatomic, retain) NSSet *favoriteColors;
+@property (nonatomic, retain) NSURL *avatarURL;
@property (nonatomic, copy) NSString *hashedString;
@property (nonatomic, retain) JTUserTest *hashedUser;
View
1 JTObjectMappingTests/JTUserTest.m
@@ -17,6 +17,7 @@ @implementation JTUserTest
@synthesize createDate;
@synthesize childs, users;
@synthesize autoString, autoArray;
+@synthesize avatarURL;
//@synthesize autoSocialNetwork;
@synthesize socialNetwork;
@synthesize nestedArray;

0 comments on commit 73f0a0e

Please sign in to comment.
Something went wrong with that request. Please try again.