Skip to content

Commit

Permalink
Merge pull request rentzsch#131 from echoz/master
Browse files Browse the repository at this point in the history
[NEW] Entity-level User Info Key/Value pairs are generated as const Structs. Use it to aid parsing JSON dictionaries or perhaps specify an entity's REST-based URL. (Jeremy Foo)
  • Loading branch information
rentzsch committed Mar 17, 2013
2 parents 0912545 + e273fa7 commit 49e13ac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
33 changes: 33 additions & 0 deletions mogenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ - (NSDictionary*)fetchedPropertiesByName {
}
@end

@interface NSEntityDescription (userInfoAdditions)
- (BOOL)hasUserInfoKeys;
- (NSDictionary *)userInfoByKeys;
@end

@implementation NSEntityDescription (userInfoAdditions)
- (BOOL)hasUserInfoKeys {
return ([self.userInfo count] > 0);
}

- (NSDictionary *)userInfoByKeys
{
NSMutableDictionary *userInfoByKeys = [NSMutableDictionary dictionary];

for (NSString *key in self.userInfo)
[userInfoByKeys setObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"key", [self.userInfo objectForKey:key], @"value", nil] forKey:key];

return userInfoByKeys;
}
@end

@implementation NSManagedObjectModel (entitiesWithACustomSubclassVerbose)
- (NSArray*)entitiesWithACustomSubclassInConfiguration:(NSString*)configuration_ verbose:(BOOL)verbose_ {
NSMutableArray *result = [NSMutableArray array];
Expand Down Expand Up @@ -161,6 +182,18 @@ - (NSArray*)noninheritedRelationships {
return [[[self relationshipsByName] allValues] sortedArrayUsingDescriptors:sortDescriptors];
}
}
/** @TypeInfo NSEntityUserInfoDescription */
- (NSArray*)userInfoKeyValues {
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"key" ascending:YES]];
NSEntityDescription *superentity = [self superentity];
if (superentity) {
NSMutableArray *result = [[[[self userInfoByKeys] allValues] mutableCopy] autorelease];
[result removeObjectsInArray:[[superentity userInfoByKeys] allValues]];
return [result sortedArrayUsingDescriptors:sortDescriptors];
} else {
return [[[self userInfoByKeys] allValues] sortedArrayUsingDescriptors:sortDescriptors];
}
}
/** @TypeInfo NSFetchedPropertyDescription */
- (NSArray*)noninheritedFetchedProperties {
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];
Expand Down
6 changes: 6 additions & 0 deletions templates/machine.h.motemplate
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ extern const struct <$managedObjectClassName$>FetchedProperties {<$foreach Fetch
} <$managedObjectClassName$>FetchedProperties;
<$endif$>

<$if hasUserInfoKeys$>
extern const struct <$managedObjectClassName$>UserInfo {<$foreach UserInfo userInfoKeyValues do$>
<$if TemplateVar.arc$>__unsafe_unretained<$endif$> NSString *<$UserInfo.key$>;<$endforeach do$>
} <$managedObjectClassName$>UserInfo;
<$endif$>

<$foreach Relationship noninheritedRelationships do$>@class <$Relationship.destinationEntity.managedObjectClassName$>;
<$endforeach do$>
<$foreach Attribute noninheritedAttributes do$>
Expand Down
6 changes: 6 additions & 0 deletions templates/machine.m.motemplate
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ const struct <$managedObjectClassName$>FetchedProperties <$managedObjectClassNam
};
<$endif$>

<$if hasUserInfoKeys$>
const struct <$managedObjectClassName$>UserInfo <$managedObjectClassName$>UserInfo = {<$foreach UserInfo userInfoKeyValues do$>
.<$UserInfo.key$> = @"<$UserInfo.value$>",<$endforeach do$>
};
<$endif$>

@implementation <$managedObjectClassName$>ID
@end

Expand Down

0 comments on commit 49e13ac

Please sign in to comment.