Skip to content

Commit

Permalink
Added meta to resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Holtz committed Jun 16, 2016
1 parent 2567863 commit 29244f3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Classes/JSONAPIResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,30 @@
*/
- (void)setID:(id)identifier;

/**
* Get the meta for a resource instance. Optional for resources that come
* from persistance storage (i.e. the server).
*
* In general, this should be implemented by a @property ID in the realized class. The
* @property declaration will automatically synthesize the get/set members declared in this
* protocol. The property storage is an implementation detail, which is why the protocol does
* not use a @property declaration.
*
* @return The meta for a resource instance.
*/
- (NSDictionary*)meta;

/**
* Set the meta for a resource instance. Optional for resources that come
* from persistance storage (i.e. the server).
*
* In general, this should be implemented by a @property ID in the realized class. The
* @property declaration will automatically synthesize the get/set members declared in this
* protocol. The property storage is an implementation detail, which is why the protocol does
* not use a @property declaration.
*
* @param meta The meta for a resource instance.
*/
- (void)setMeta:(NSDictionary*)meta;

@end
6 changes: 6 additions & 0 deletions Classes/JSONAPIResourceBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@
*/
@property (strong, atomic) id ID;

/**
* Meta for a resource instance. Optional for resources that come
* from persistance storage (i.e. the server).
*/
@property (strong, atomic) NSDictionary *meta;

@end
5 changes: 5 additions & 0 deletions Classes/JSONAPIResourceParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {
NSDictionary *relationships = [dictionary objectForKey:@"relationships"];
NSDictionary *attributes = [dictionary objectForKey:@"attributes"];
NSDictionary *links = [dictionary objectForKey:@"links"];
NSDictionary *meta = [dictionary objectForKey:@"meta"];

id ID = [dictionary objectForKey:@"id"];
NSFormatter *format = [descriptor idFormatter];
Expand All @@ -197,6 +198,10 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {
NSString *selfLink = links[@"self"];
[resource setValue:selfLink forKey:descriptor.selfLinkProperty];
}

if ([resource respondsToSelector:@selector(setMeta:)]) {
[resource setMeta:meta];
}

// Loops through all keys to map to properties
NSDictionary *properties = [descriptor properties];
Expand Down
4 changes: 4 additions & 0 deletions Project/JSONAPITests/JSONAPITests.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ - (void)testDataArticles {
XCTAssertEqual(jsonAPI.resources.count, 1, @"Resources should contain 1 resource");

ArticleResource *article = jsonAPI.resource;

XCTAssertNotNil(article.meta, @"Meta should not be nil");
XCTAssertEqualObjects(article.meta[@"hehe"], @"hoho", @"Meta's 'hehe' should equal 'hoho'");

XCTAssert([article isKindOfClass:[ArticleResource class]], @"Article should be a ArticleResource");
XCTAssertEqualObjects(article.ID, @"1", @"Article id should be 1");
XCTAssertTrue([article.selfLink isEqualToString:@"http://example.com/articles/1"], @"Article selfLink should be 'http://example.com/articles/1'");
Expand Down
3 changes: 3 additions & 0 deletions Project/JSONAPITests/main_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"data": [{
"type": "articles",
"id": "1",
"meta": {
"hehe": "hoho"
},
"attributes": {
"title": "JSON API paints my bikeshed!",
"versions": [
Expand Down

0 comments on commit 29244f3

Please sign in to comment.