Skip to content

Commit 50b19a9

Browse files
author
Josh Holtz
committed
Add resource formatter and fixed NSCopying
1 parent 651f88f commit 50b19a9

13 files changed

+186
-72
lines changed

Classes/JSONAPI.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import <Foundation/Foundation.h>
1010

1111
#import "JSONAPIResource.h"
12+
#import "JSONAPIResourceFormatter.h"
1213
#import "JSONAPIResourceLinker.h"
1314
#import "JSONAPIResourceModeler.h"
1415

Classes/JSONAPIResource.m

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "JSONAPIResource.h"
1010

11+
#import "JSONAPIResourceFormatter.h"
1112
#import "JSONAPIResourceLinker.h"
1213

1314
#import <objc/runtime.h>
@@ -24,6 +25,9 @@ @interface JSONAPIResource()
2425

2526
@implementation JSONAPIResource
2627

28+
#pragma mark -
29+
#pragma mark - Class Methods
30+
2731
+ (NSArray*)jsonAPIResources:(NSArray*)array withLinked:(NSDictionary*)linked {
2832
return [JSONAPIResource jsonAPIResources:array withLinked:linked withClass:[self class]];
2933
}
@@ -53,6 +57,9 @@ + (id)jsonAPIResource:(NSDictionary*)dictionary withLinked:(NSDictionary*)linked
5357
return [[resourceObjectClass alloc] initWithDictionary:dictionary withLinked:linked];
5458
}
5559

60+
#pragma mark -
61+
#pragma mark - Instance Methods
62+
5663
- (id)init {
5764
self = [super init];
5865
if (self) {
@@ -110,7 +117,10 @@ - (void)setWithDictionary:(NSDictionary*)dict {
110117
if (inflateRange.location != NSNotFound) {
111118

112119
} else if (formatRange.location != NSNotFound) {
113-
120+
NSString *formatFunction = [property substringToIndex:formatRange.location];
121+
property = [property substringFromIndex:(formatRange.location+1)];
122+
123+
[self setValue:[JSONAPIResourceFormatter performFormatBlock:[dict objectForKey:key] withName:formatFunction] forKey:property ];
114124
} else {
115125
[self setValue:[dict objectForKey:key] forKey:property ];
116126
}
@@ -167,6 +177,7 @@ - (void)linkLinks:(NSDictionary*)linked {
167177

168178
id resource = [self linkedResourceForKey:linkedResource];
169179
if (resource != nil) {
180+
170181
@try {
171182
[self setValue:resource forKey:propertyName];
172183
}
@@ -186,7 +197,19 @@ - (id)copyWithZone:(NSZone *)zone {
186197

187198
if (copy) {
188199
// Copy NSObject subclasses
200+
NSLog(@"__resourceLinks - %@", self.__resourceLinks);
189201
[copy set__resourceLinks:[self.__resourceLinks copyWithZone:zone]];
202+
203+
// Link links for mapped key to properties
204+
for (NSString *key in [copy __resourceLinks]) {
205+
@try {
206+
[copy setValue:[[copy __resourceLinks] objectForKey:key] forKey:key];
207+
}
208+
@catch (NSException *exception) {
209+
NSLog(@"JSONAPIResource Warning - %@", [exception description]);
210+
}
211+
}
212+
190213
}
191214

192215
return copy;

Classes/JSONAPIResourceFormatter.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// JSONAPIResourceFormatter.h
3+
// JSONAPI
4+
//
5+
// Created by Josh Holtz on 7/9/14.
6+
// Copyright (c) 2014 Josh Holtz. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface JSONAPIResourceFormatter : NSObject
12+
13+
+ (void)registerFormat:(NSString*)name withBlock:(id(^)(id jsonValue))block;
14+
+ (id)performFormatBlock:(NSString*)value withName:(NSString*)name;
15+
16+
@end

Classes/JSONAPIResourceFormatter.m

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// JSONAPIResourceFormatter.m
3+
// JSONAPI
4+
//
5+
// Created by Josh Holtz on 7/9/14.
6+
// Copyright (c) 2014 Josh Holtz. All rights reserved.
7+
//
8+
9+
#import "JSONAPIResourceFormatter.h"
10+
11+
@interface JSONAPIResourceFormatter()
12+
13+
@property (nonatomic, strong) NSMutableDictionary *formatBlocks;
14+
15+
@end
16+
17+
@implementation JSONAPIResourceFormatter
18+
19+
+ (instancetype)sharedFormatter {
20+
static JSONAPIResourceFormatter *_sharedFormatter = nil;
21+
static dispatch_once_t onceToken;
22+
dispatch_once(&onceToken, ^{
23+
_sharedFormatter = [[JSONAPIResourceFormatter alloc] init];
24+
});
25+
26+
return _sharedFormatter;
27+
}
28+
29+
- (id)init {
30+
self = [super init];
31+
if (self) {
32+
self.formatBlocks = @{}.mutableCopy;
33+
}
34+
return self;
35+
}
36+
37+
+ (void)registerFormat:(NSString*)name withBlock:(id(^)(id jsonValue))block {
38+
[[JSONAPIResourceFormatter sharedFormatter].formatBlocks setObject:[block copy] forKey:name];
39+
}
40+
41+
+ (id)performFormatBlock:(NSString*)value withName:(NSString*)name {
42+
id(^block)(NSString *);
43+
block = [[JSONAPIResourceFormatter sharedFormatter].formatBlocks objectForKey:name];
44+
if (block != nil) {
45+
return block(value);
46+
} else {
47+
return nil;
48+
}
49+
}
50+
51+
@end

Project/JSONAPI.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
03A055E61868E038004807F0 /* JSONAPITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 03A055E51868E038004807F0 /* JSONAPITests.m */; };
3131
03A055F71868E08A004807F0 /* JSONAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = 03A055F41868E08A004807F0 /* JSONAPI.m */; };
3232
03A055F81868E08A004807F0 /* JSONAPI.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 03A055F51868E08A004807F0 /* JSONAPI.podspec */; };
33+
03A9ED58196DEF9B00E61E2E /* JSONAPIResourceFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 03A9ED57196DEF9B00E61E2E /* JSONAPIResourceFormatter.m */; };
3334
/* End PBXBuildFile section */
3435

3536
/* Begin PBXContainerItemProxy section */
@@ -78,6 +79,8 @@
7879
03A055F31868E08A004807F0 /* JSONAPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONAPI.h; sourceTree = "<group>"; };
7980
03A055F41868E08A004807F0 /* JSONAPI.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONAPI.m; sourceTree = "<group>"; };
8081
03A055F51868E08A004807F0 /* JSONAPI.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = JSONAPI.podspec; path = ../JSONAPI.podspec; sourceTree = "<group>"; };
82+
03A9ED56196DEF9B00E61E2E /* JSONAPIResourceFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONAPIResourceFormatter.h; sourceTree = "<group>"; };
83+
03A9ED57196DEF9B00E61E2E /* JSONAPIResourceFormatter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONAPIResourceFormatter.m; sourceTree = "<group>"; };
8184
/* End PBXFileReference section */
8285

8386
/* Begin PBXFrameworksBuildPhase section */
@@ -201,6 +204,8 @@
201204
03A055F41868E08A004807F0 /* JSONAPI.m */,
202205
033A6C5D18695CD2001CF9FA /* JSONAPIResource.h */,
203206
033A6C5E18695CD2001CF9FA /* JSONAPIResource.m */,
207+
03A9ED56196DEF9B00E61E2E /* JSONAPIResourceFormatter.h */,
208+
03A9ED57196DEF9B00E61E2E /* JSONAPIResourceFormatter.m */,
204209
0386643B186A0DAD00985CEC /* JSONAPIResourceLinker.h */,
205210
0386643C186A0DAD00985CEC /* JSONAPIResourceLinker.m */,
206211
0386644C186A8DA000985CEC /* JSONAPIResourceModeler.h */,
@@ -316,6 +321,7 @@
316321
03A055F71868E08A004807F0 /* JSONAPI.m in Sources */,
317322
033A6C5F18695CD2001CF9FA /* JSONAPIResource.m in Sources */,
318323
03866454186A94B700985CEC /* CommentResource.m in Sources */,
324+
03A9ED58196DEF9B00E61E2E /* JSONAPIResourceFormatter.m in Sources */,
319325
03A055CB1868E038004807F0 /* AppDelegate.m in Sources */,
320326
0386644E186A8DA000985CEC /* JSONAPIResourceModeler.m in Sources */,
321327
03A055C71868E038004807F0 /* main.m in Sources */,

Project/JSONAPI/CommentResource.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
@interface CommentResource : JSONAPIResource
1212

13-
@property (nonatomic, strong) NSString *mapText;
14-
15-
- (NSString*)text;
13+
@property (nonatomic, strong) NSString *text;
1614

1715
@end

Project/JSONAPI/CommentResource.m

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@
1010

1111
@implementation CommentResource
1212

13-
- (NSString *)text {
14-
return [self objectForKey:@"text"];
15-
}
16-
1713
- (NSDictionary *)mapKeysToProperties {
1814
return @{
19-
@"text" : @"mapText"
15+
@"text" : @"text"
2016
};
2117
}
2218

Project/JSONAPI/PeopleResource.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
@interface PeopleResource : JSONAPIResource
1212

13-
@property (nonatomic, strong) NSString *mapName;
14-
15-
- (NSString*)name;
13+
@property (nonatomic, strong) NSString *name;
1614

1715
@end

Project/JSONAPI/PeopleResource.m

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@
1010

1111
@implementation PeopleResource
1212

13-
- (NSString *)name {
14-
return [self objectForKey:@"name"];
15-
}
16-
1713
- (NSDictionary *)mapKeysToProperties {
1814
return @{
19-
@"name" : @"mapName"
15+
@"name" : @"name"
2016
};
2117
}
2218

Project/JSONAPI/PostResource.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@
1313

1414
@interface PostResource : JSONAPIResource
1515

16-
@property (nonatomic, strong) NSString *mapName;
17-
@property (nonatomic, strong) PeopleResource *mapAuthor;
18-
@property (nonatomic, strong) NSArray *mapComments;
19-
20-
- (PeopleResource*)author;
21-
- (NSArray*)comments;
22-
- (NSString*)name;
16+
@property (nonatomic, strong) NSString *name;
17+
@property (nonatomic, strong) PeopleResource *author;
18+
@property (nonatomic, strong) NSDate *date;
19+
@property (nonatomic, strong) NSArray *comments;
2320

2421
@end

0 commit comments

Comments
 (0)