From e12bea858965289d0dcf03b3d8f8ae58eb2f1339 Mon Sep 17 00:00:00 2001 From: Jeremy Foo Date: Tue, 2 Oct 2012 21:47:58 +0800 Subject: [PATCH 1/5] get user info key value pairs --- mogenerator.m | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mogenerator.m b/mogenerator.m index f2739bc6..951794f8 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -30,6 +30,22 @@ - (NSDictionary *)fetchedPropertiesByName } @end +@interface NSEntityDescription (userInfoAdditions) +- (NSDictionary *)userInfoByKeys; +@end + +@implementation NSEntityDescription (userInfoAdditions) +- (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]; @@ -126,6 +142,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]]; From 9970d392cd03d8532460a49574a605fde200b3b9 Mon Sep 17 00:00:00 2001 From: Jeremy Foo Date: Tue, 2 Oct 2012 21:56:40 +0800 Subject: [PATCH 2/5] template for generating userinfo key value pairs --- templates/machine.h.motemplate | 4 ++++ templates/machine.m.motemplate | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/templates/machine.h.motemplate b/templates/machine.h.motemplate index 54ebb028..09c9a428 100644 --- a/templates/machine.h.motemplate +++ b/templates/machine.h.motemplate @@ -16,6 +16,10 @@ extern const struct <$managedObjectClassName$>FetchedProperties {<$foreach Fetch <$if TemplateVar.arc$>__unsafe_unretained<$endif$> NSString *<$FetchedProperty.name$>;<$endforeach do$> } <$managedObjectClassName$>FetchedProperties; +extern const struct <$managedObjectClassName$>UserInfo {<$foreach UserInfo userInfoKeyValues do$> + <$if TemplateVar.arc$>__unsafe_unretained<$endif$> NSString *<$UserInfo.key$>;<$endforeach do$> +} <$managedObjectClassName$>UserInfo; + <$foreach Relationship noninheritedRelationships do$>@class <$Relationship.destinationEntity.managedObjectClassName$>; <$endforeach do$> <$foreach Attribute noninheritedAttributes do$><$if Attribute.hasTransformableAttributeType$>@class <$Attribute.objectAttributeClassName$>;<$endif$> diff --git a/templates/machine.m.motemplate b/templates/machine.m.motemplate index a4ddc399..78cc915a 100644 --- a/templates/machine.m.motemplate +++ b/templates/machine.m.motemplate @@ -15,6 +15,10 @@ const struct <$managedObjectClassName$>FetchedProperties <$managedObjectClassNam .<$FetchedProperty.name$> = @"<$FetchedProperty.name$>",<$endforeach do$> }; +const struct <$managedObjectClassName$>UserInfo <$managedObjectClassName$>UserInfo {<$foreach UserInfo userInfoKeyValues do$> + .<$UserInfo.key$> = @"<$UserInfo.value$>",<$endforeach do$> +}; + @implementation <$managedObjectClassName$>ID @end From 567c33c6e749122fb4c98e3b0a447b67ad06cc6c Mon Sep 17 00:00:00 2001 From: Jeremy Foo Date: Tue, 2 Oct 2012 22:07:07 +0800 Subject: [PATCH 3/5] forgotten a simple = --- templates/machine.m.motemplate | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/machine.m.motemplate b/templates/machine.m.motemplate index 78cc915a..8fcb1a84 100644 --- a/templates/machine.m.motemplate +++ b/templates/machine.m.motemplate @@ -15,7 +15,7 @@ const struct <$managedObjectClassName$>FetchedProperties <$managedObjectClassNam .<$FetchedProperty.name$> = @"<$FetchedProperty.name$>",<$endforeach do$> }; -const struct <$managedObjectClassName$>UserInfo <$managedObjectClassName$>UserInfo {<$foreach UserInfo userInfoKeyValues do$> +const struct <$managedObjectClassName$>UserInfo <$managedObjectClassName$>UserInfo = {<$foreach UserInfo userInfoKeyValues do$> .<$UserInfo.key$> = @"<$UserInfo.value$>",<$endforeach do$> }; From 8281ff332cd222ccbb84f94c02908c3cd8df0234 Mon Sep 17 00:00:00 2001 From: Jeremy Foo Date: Thu, 17 Jan 2013 00:14:34 +0800 Subject: [PATCH 4/5] - Detects if the user info dictionary is empty and provides a way for Misc Merge to use as a conditional. - Updated templates to not emit structs when the user info dictionary is empty. --- mogenerator.m | 4 ++++ templates/machine.h.motemplate | 2 ++ templates/machine.m.motemplate | 2 ++ 3 files changed, 8 insertions(+) diff --git a/mogenerator.m b/mogenerator.m index 951794f8..f0adc224 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -35,6 +35,10 @@ - (NSDictionary *)userInfoByKeys; @end @implementation NSEntityDescription (userInfoAdditions) +- (BOOL)hasUserInfoKeys { + return ([self.userInfo count] > 0); +} + - (NSDictionary *)userInfoByKeys { NSMutableDictionary *userInfoByKeys = [NSMutableDictionary dictionary]; diff --git a/templates/machine.h.motemplate b/templates/machine.h.motemplate index 09c9a428..65fa8e7e 100644 --- a/templates/machine.h.motemplate +++ b/templates/machine.h.motemplate @@ -16,9 +16,11 @@ extern const struct <$managedObjectClassName$>FetchedProperties {<$foreach Fetch <$if TemplateVar.arc$>__unsafe_unretained<$endif$> NSString *<$FetchedProperty.name$>;<$endforeach do$> } <$managedObjectClassName$>FetchedProperties; +<$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$> diff --git a/templates/machine.m.motemplate b/templates/machine.m.motemplate index 8fcb1a84..e6d398b2 100644 --- a/templates/machine.m.motemplate +++ b/templates/machine.m.motemplate @@ -15,9 +15,11 @@ const struct <$managedObjectClassName$>FetchedProperties <$managedObjectClassNam .<$FetchedProperty.name$> = @"<$FetchedProperty.name$>",<$endforeach do$> }; +<$if hasUserInfoKeys$> const struct <$managedObjectClassName$>UserInfo <$managedObjectClassName$>UserInfo = {<$foreach UserInfo userInfoKeyValues do$> .<$UserInfo.key$> = @"<$UserInfo.value$>",<$endforeach do$> }; +<$endif$> @implementation <$managedObjectClassName$>ID @end From 4ca4405a28cbb17055dc8d0bfc6bc2fe60c426ed Mon Sep 17 00:00:00 2001 From: Jeremy Foo Date: Thu, 17 Jan 2013 00:16:42 +0800 Subject: [PATCH 5/5] Forgotten to include in the @interface section --- mogenerator.m | 1 + 1 file changed, 1 insertion(+) diff --git a/mogenerator.m b/mogenerator.m index f0adc224..6c623681 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -31,6 +31,7 @@ - (NSDictionary *)fetchedPropertiesByName @end @interface NSEntityDescription (userInfoAdditions) +- (BOOL)hasUserInfoKeys; - (NSDictionary *)userInfoByKeys; @end