Skip to content

Commit 6f775ee

Browse files
author
Josh Holtz
committed
Added meta to resource
1 parent 2567863 commit 6f775ee

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

Classes/JSONAPIResource.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,30 @@
135135
*/
136136
- (void)setID:(id)identifier;
137137

138+
/**
139+
* Get the meta for a resource instance. Optional for resources that come
140+
* from persistance storage (i.e. the server).
141+
*
142+
* In general, this should be implemented by a @property ID in the realized class. The
143+
* @property declaration will automatically synthesize the get/set members declared in this
144+
* protocol. The property storage is an implementation detail, which is why the protocol does
145+
* not use a @property declaration.
146+
*
147+
* @return The meta for a resource instance.
148+
*/
149+
- (NSDictionary*)meta;
150+
151+
/**
152+
* Set the meta for a resource instance. Optional for resources that come
153+
* from persistance storage (i.e. the server).
154+
*
155+
* In general, this should be implemented by a @property ID in the realized class. The
156+
* @property declaration will automatically synthesize the get/set members declared in this
157+
* protocol. The property storage is an implementation detail, which is why the protocol does
158+
* not use a @property declaration.
159+
*
160+
* @param meta The meta for a resource instance.
161+
*/
162+
- (void)setMeta:(NSDictionary*)meta;
138163

139164
@end

Classes/JSONAPIResourceBase.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@
4343
*/
4444
@property (strong, atomic) id ID;
4545

46+
/**
47+
* Meta for a resource instance. Optional for resources that come
48+
* from persistance storage (i.e. the server).
49+
*/
50+
@property (strong, atomic) NSDictionary *meta;
51+
4652
@end

Classes/JSONAPIResourceParser.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {
181181
NSDictionary *relationships = [dictionary objectForKey:@"relationships"];
182182
NSDictionary *attributes = [dictionary objectForKey:@"attributes"];
183183
NSDictionary *links = [dictionary objectForKey:@"links"];
184+
NSDictionary *meta = [dictionary objectForKey:@"meta"];
184185

185186
id ID = [dictionary objectForKey:@"id"];
186187
NSFormatter *format = [descriptor idFormatter];
@@ -197,6 +198,10 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {
197198
NSString *selfLink = links[@"self"];
198199
[resource setValue:selfLink forKey:descriptor.selfLinkProperty];
199200
}
201+
202+
if ([resource respondsToSelector:@selector(setMeta:)]) {
203+
[resource setMeta:meta];
204+
}
200205

201206
// Loops through all keys to map to properties
202207
NSDictionary *properties = [descriptor properties];

Project/JSONAPITests/JSONAPITests.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ - (void)testDataArticles {
6464
XCTAssertEqual(jsonAPI.resources.count, 1, @"Resources should contain 1 resource");
6565

6666
ArticleResource *article = jsonAPI.resource;
67+
68+
XCTAssertNotNil(article.meta, @"Meta should not be nil");
69+
XCTAssertEqualObjects(article.meta[@"hehe"], @"hoho", @"Meta's 'hehe' should equal 'hoho'");
70+
6771
XCTAssert([article isKindOfClass:[ArticleResource class]], @"Article should be a ArticleResource");
6872
XCTAssertEqualObjects(article.ID, @"1", @"Article id should be 1");
6973
XCTAssertTrue([article.selfLink isEqualToString:@"http://example.com/articles/1"], @"Article selfLink should be 'http://example.com/articles/1'");

Project/JSONAPITests/main_example.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"data": [{
1111
"type": "articles",
1212
"id": "1",
13+
"meta": {
14+
"hehe": "hoho"
15+
},
1316
"attributes": {
1417
"title": "JSON API paints my bikeshed!",
1518
"versions": [

0 commit comments

Comments
 (0)