Skip to content

Commit c2c9290

Browse files
author
rokkincat
committed
Minor fixes
1 parent c5dbfb1 commit c2c9290

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

Classes/JSONAPI.m

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,27 @@ - (id)objectForKey:(NSString*)key {
5959
}
6060

6161
- (id)resourceForKey:(NSString*)key {
62-
JSONAPIResource *resource;
62+
if ([key isEqualToString:@"meta"] == YES || [key isEqualToString:@"linked"] == YES) {
63+
return nil;
64+
}
65+
66+
NSDictionary *rawResource = [_dictionary objectForKey:key];
67+
JSONAPIResource *resource = nil;
68+
if ([rawResource isKindOfClass:[NSDictionary class]] == YES) {
69+
Class c = [JSONAPIResourceModeler resourceForLinkedType:[JSONAPIResourceLinker linkedType:key]];
70+
resource = [JSONAPIResource jsonAPIResource:rawResource withLinked:self.linked withClass:c];
71+
}
6372

64-
NSArray *resources = [self resourcesForKey:key];
65-
if (resources.count > 0) {
66-
return [resources objectAtIndex:0];
73+
// Fall back to first element in array
74+
if (resource == nil) {
75+
id resources = [self resourcesForKey:key];
76+
if ([resources isKindOfClass:[NSArray class]] == YES) {
77+
return [resources firstObject];
78+
}
6779
}
6880

6981
return resource;
82+
7083
}
7184

7285
- (NSArray*)resourcesForKey:(NSString*)key {

Classes/JSONAPIResource.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
+ (id)jsonAPIResource:(NSDictionary*)dictionary withLinked:(NSDictionary*)linked withClass:(Class)resourceObjectClass;
2222

2323
- (id)initWithDictionary:(NSDictionary*)dict withLinked:(NSDictionary*)linked;
24+
- (void)setWithDictionary:(NSDictionary*)dict;
2425

2526
- (id)objectForKey:(NSString*)key;
2627
- (id)linkedResourceForKey:(NSString*)key;

Classes/JSONAPIResource.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ - (NSDictionary *)mapKeysToProperties {
9090
return [[NSDictionary alloc] init];
9191
}
9292

93+
- (BOOL)setWithResource:(id)otherResource {
94+
if ([otherResource isKindOfClass:[self class]] == YES) {
95+
96+
return YES;
97+
}
98+
99+
return NO;
100+
}
101+
93102
- (void)setWithDictionary:(NSDictionary*)dict {
94103
self.__dictionary = dict;
95104

@@ -148,9 +157,7 @@ - (void)linkLinks:(NSDictionary*)linked {
148157
// Gets linked objects for the resource
149158
id linksTo = [self.links objectForKey:linkTypeUnmapped];
150159
if ([linksTo isKindOfClass:[NSNumber class]] == YES || [linksTo isKindOfClass:[NSString class]] == YES) {
151-
152160
JSONAPIResource *linkedResource = [[linked objectForKey:linkType] objectForKey:linksTo];
153-
154161
if (linkedResource != nil) {
155162
[self.__resourceLinks setObject:linkedResource forKey:linkTypeUnmapped];
156163
}

0 commit comments

Comments
 (0)