Skip to content

Commit 5cbc8fc

Browse files
author
Josh Holtz
committed
Linked resources with links actually link
1 parent e52e136 commit 5cbc8fc

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

Classes/JSONAPI.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ - (void)inflateWithDictionary:(NSDictionary*)dictionary {
101101
NSDictionary *rawLinked = [dictionary objectForKey:@"linked"];
102102
if ([rawLinked isKindOfClass:[NSDictionary class]] == YES) {
103103

104+
NSMutableArray *linkedToLinkWithLinked = [NSMutableArray array];
105+
104106
// Loops through linked arrays
105107
for (NSString *key in rawLinked.allKeys) {
106108
NSArray *value = [rawLinked objectForKey:key];
@@ -111,14 +113,22 @@ - (void)inflateWithDictionary:(NSDictionary*)dictionary {
111113
Class c = [JSONAPIResourceModeler resourceForLinkedType:[JSONAPIResourceLinker linkedType:key]];
112114
JSONAPIResource *resource = [JSONAPIResource jsonAPIResource:resourceDictionary withLinked:nil withClass:c];
113115
[resources setObject:resource forKey:resource.ID];
116+
117+
[linkedToLinkWithLinked addObject:resource];
114118
}
115119
[creatingLinked setObject:resources forKey:key];
116120

117121
}
118122

119123
}
120124

125+
// Linked the linked
126+
for (JSONAPIResource *resource in linkedToLinkWithLinked) {
127+
[resource linkLinks:creatingLinked];
128+
}
129+
121130
}
131+
122132
_linked = creatingLinked;
123133
}
124134

Classes/JSONAPIResource.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@
2323
- (id)objectForKey:(NSString*)key;
2424
- (id)linkedResourceForKey:(NSString*)key;
2525

26+
- (void)linkLinks:(NSDictionary*)linked;
27+
2628
@end

Classes/JSONAPIResource.m

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ - (id)initWithDictionary:(NSDictionary*)dict withLinked:(NSDictionary*)linked {
6262
self = [self init];
6363
if (self) {
6464
[self setWithDictionary:dict];
65-
[self setWithLinks:linked];
65+
[self linkLinks:linked];
6666
}
6767
return self;
6868
}
@@ -104,28 +104,9 @@ - (void)setWithDictionary:(NSDictionary*)dict {
104104

105105
@try {
106106
if (inflateRange.location != NSNotFound) {
107-
// NSString *object = [property substringToIndex:inflateRange.location];
108-
// property = [property substringFromIndex:(inflateRange.location+1)];
109-
//
110-
// Class class = NSClassFromString(object);
111-
// if ([[dict objectForKey:key] isKindOfClass:[NSDictionary class]]) {
112-
// ProtocolModel *obj = [[class alloc] initWithDictionary:[dict objectForKey:key]];
113-
// [obj setParentModel:self];
114-
//
115-
// [self setValue:obj forKey:property];
116-
// } else if ([[dict objectForKey:key] isKindOfClass:[NSArray class]]) {
117-
// NSArray *array = [ProtocolModel protocolModels:[dict objectForKey:key] withClass:class];
118-
// for (ProtocolModel *model in array) {
119-
// [model setParentModel:self];
120-
// }
121-
//
122-
// [self setValue:array forKey:property];
123-
// }
107+
124108
} else if (formatRange.location != NSNotFound) {
125-
// NSString *formatFunction = [property substringToIndex:formatRange.location];
126-
// property = [property substringFromIndex:(formatRange.location+1)];
127-
//
128-
// [self setValue:[[ProtocolModel sharedInstance] performFormatBlock:[dict objectForKey:key] withKey:formatFunction] forKey:property ];
109+
129110
} else {
130111
[self setValue:[dict objectForKey:key] forKey:property ];
131112
}
@@ -141,7 +122,7 @@ - (void)setWithDictionary:(NSDictionary*)dict {
141122
}
142123
}
143124

144-
- (void)setWithLinks:(NSDictionary*)linked {
125+
- (void)linkLinks:(NSDictionary*)linked {
145126
// Loops through links of resources
146127
for (NSString *linkTypeUnmapped in self.links.allKeys) {
147128

@@ -155,7 +136,9 @@ - (void)setWithLinks:(NSDictionary*)linked {
155136
if ([linksTo isKindOfClass:[NSNumber class]] == YES) {
156137
JSONAPIResource *linkedResource = [[linked objectForKey:linkType] objectForKey:linksTo];
157138

158-
[self.__resourceLinks setObject:linkedResource forKey:linkTypeUnmapped];
139+
if (linkedResource != nil) {
140+
[self.__resourceLinks setObject:linkedResource forKey:linkTypeUnmapped];
141+
}
159142

160143
} else if ([linksTo isKindOfClass:[NSArray class]] == YES) {
161144
NSMutableArray *linkedResources = [NSMutableArray array];

Project/JSONAPITests/JSONAPITests.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ - (void)setUp
2727
[JSONAPIResourceLinker link:@"author" toLinkedType:@"authors"];
2828
[JSONAPIResourceLinker link:@"authors" toLinkedType:@"authors"]; // Don't NEED this but why not be explicit
2929
[JSONAPIResourceLinker link:@"person" toLinkedType:@"people"];
30+
[JSONAPIResourceLinker link:@"chapter" toLinkedType:@"chapters"];
31+
[JSONAPIResourceLinker link:@"book" toLinkedType:@"books"];
3032

3133
[JSONAPIResourceModeler useResource:[CommentResource class] toLinkedType:@"comment"];
3234
[JSONAPIResourceModeler useResource:[PeopleResource class] toLinkedType:@"authors"];
@@ -223,4 +225,26 @@ - (void)testResourceModels {
223225
XCTAssertEqualObjects(postResource.author.name, [linkedAuthor9 objectForKey:@"name"], @"Author name is not equal to %@", [post objectForKey:@"name"]);
224226
}
225227

228+
- (void)testLinksInLinked {
229+
NSDictionary *meta = @{};
230+
NSDictionary *linkedBook9 = @{ @"id" : @9, @"name" : @"A Book That's A Book", @"links" : @{ @"author" : @11 } };
231+
NSArray *linkedBooks = @[ linkedBook9 ];
232+
NSDictionary *linkedAuthor11 = @{ @"id" : @11, @"name" : @"Bandit"};
233+
NSArray *linkedAuthors = @[ linkedAuthor11 ];
234+
NSDictionary *linked = @{ @"authors" : linkedAuthors, @"books" : linkedBooks};
235+
NSDictionary *chapter = @{ @"id" : @1, @"name" : @"Chapter 1: And It Begins", @"links" : @{ @"book" : @9 } };
236+
NSArray *chapters = @[ chapter ];
237+
NSDictionary *json = @{ @"meta" : meta, @"linked" : linked, @"chapters" : chapters };
238+
239+
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
240+
241+
JSONAPIResource *chapterResource = [jsonAPI resourceForKey:@"chapters"];
242+
JSONAPIResource *bookResource = [chapterResource linkedResourceForKey:@"book"];
243+
JSONAPIResource *authorResource = [bookResource linkedResourceForKey:@"author"];
244+
245+
XCTAssertEqualObjects([chapterResource objectForKey:@"name"], [chapter objectForKey:@"name"], @"Chapter name should equal %@", [chapter objectForKey:@"name"]);
246+
XCTAssertEqualObjects([bookResource objectForKey:@"name"], [linkedBook9 objectForKey:@"name"], @"Book name should equal %@", [chapter objectForKey:@"name"]);
247+
XCTAssertEqualObjects([authorResource objectForKey:@"name"], [linkedAuthor11 objectForKey:@"name"], @"Author name should equal %@", [chapter objectForKey:@"name"]);
248+
}
249+
226250
@end

0 commit comments

Comments
 (0)