Skip to content

Commit

Permalink
[NEW] Support for ordered relationships.
Browse files Browse the repository at this point in the history
  • Loading branch information
rentzsch committed Jul 1, 2011
1 parent 88ab27b commit 4e9a045
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
10 changes: 9 additions & 1 deletion mogenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
- (NSArray*)entitiesWithACustomSubclassVerbose:(BOOL)verbose_;
@end


@interface NSEntityDescription (customBaseClass)
- (BOOL)hasCustomSuperentity;
- (NSString*)customSuperentity;
Expand All @@ -36,6 +35,15 @@
- (BOOL)hasTransformableAttributeType;
@end

@interface NSRelationshipDescription (collectionClassName)
- (NSString*)mutableCollectionClassName;
- (NSString*)immutableCollectionClassName;
- (BOOL)_jr_isOrdered;
@end
@interface NSObject (JustHereToSuppressIsOrderedNotImplementedCompilerWarning)
- (BOOL)isOrdered;
@end

@interface NSString (camelCaseString)
- (NSString*)camelCaseString;
@end
Expand Down
20 changes: 20 additions & 0 deletions mogenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,26 @@ - (BOOL)hasTransformableAttributeType {

@end

@implementation NSRelationshipDescription (collectionClassName)

- (NSString*)mutableCollectionClassName {
return [self _jr_isOrdered] ? @"NSMutableOrderedSet" : @"NSMutableSet";
}

- (NSString*)immutableCollectionClassName {
return [self _jr_isOrdered] ? @"NSOrderedSet" : @"NSSet";
}

- (BOOL)_jr_isOrdered {
if ([self respondsToSelector:@selector(isOrdered)]) {
return [self isOrdered];
} else {
return NO;
}
}

@end

@implementation NSString (camelCaseString)
- (NSString*)camelCaseString {
NSArray *lowerCasedWordArray = [[self wordArray] arrayByMakingObjectsPerformSelector:@selector(lowercaseString)];
Expand Down
14 changes: 7 additions & 7 deletions templates/machine.h.motemplate
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
<$foreach Relationship noninheritedRelationships do$>
<$if Relationship.isToMany$>
<$if TemplateVar.arc$>
@property (nonatomic, strong) NSSet* <$Relationship.name$>;
@property (nonatomic, strong) <$Relationship.immutableCollectionClassName$>* <$Relationship.name$>;
<$else$>
@property (nonatomic, retain) NSSet* <$Relationship.name$>;
@property (nonatomic, retain) <$Relationship.immutableCollectionClassName$>* <$Relationship.name$>;
<$endif$>
- (NSMutableSet*)<$Relationship.name$>Set;
- (<$Relationship.mutableCollectionClassName$>*)<$Relationship.name$>Set;
<$else$>
<$if TemplateVar.arc$>
@property (nonatomic, strong) <$Relationship.destinationEntity.managedObjectClassName$>* <$Relationship.name$>;
Expand All @@ -65,8 +65,8 @@

@interface _<$managedObjectClassName$> (CoreDataGeneratedAccessors)
<$foreach Relationship noninheritedRelationships do$><$if Relationship.isToMany$>
- (void)add<$Relationship.name.initialCapitalString$>:(NSSet*)value_;
- (void)remove<$Relationship.name.initialCapitalString$>:(NSSet*)value_;
- (void)add<$Relationship.name.initialCapitalString$>:(<$Relationship.immutableCollectionClassName$>*)value_;
- (void)remove<$Relationship.name.initialCapitalString$>:(<$Relationship.immutableCollectionClassName$>*)value_;
- (void)add<$Relationship.name.initialCapitalString$>Object:(<$Relationship.destinationEntity.managedObjectClassName$>*)value_;
- (void)remove<$Relationship.name.initialCapitalString$>Object:(<$Relationship.destinationEntity.managedObjectClassName$>*)value_;
<$endif$><$endforeach do$>
Expand All @@ -85,8 +85,8 @@
<$endforeach do$>
<$foreach Relationship noninheritedRelationships do$>
<$if Relationship.isToMany$>
- (NSMutableSet*)primitive<$Relationship.name.initialCapitalString$>;
- (void)setPrimitive<$Relationship.name.initialCapitalString$>:(NSMutableSet*)value;
- (<$Relationship.mutableCollectionClassName$>*)primitive<$Relationship.name.initialCapitalString$>;
- (void)setPrimitive<$Relationship.name.initialCapitalString$>:(<$Relationship.mutableCollectionClassName$>*)value;
<$else$>
- (<$Relationship.destinationEntity.managedObjectClassName$>*)primitive<$Relationship.name.initialCapitalString$>;
- (void)setPrimitive<$Relationship.name.initialCapitalString$>:(<$Relationship.destinationEntity.managedObjectClassName$>*)value;
Expand Down
10 changes: 5 additions & 5 deletions templates/machine.m.motemplate
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
return (<$managedObjectClassName$>ID*)[super objectID];
}

+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
+ (<$Relationship.immutableCollectionClassName$> *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
<$Relationship.immutableCollectionClassName$> *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
<$foreach Attribute noninheritedAttributes do$><$if Attribute.hasDefinedAttributeType$><$if Attribute.hasScalarAttributeType$>
if ([key isEqualToString:@"<$Attribute.name$>Value"]) {
NSSet *affectingKey = [NSSet setWithObject:@"<$Attribute.name$>"];
<$Relationship.immutableCollectionClassName$> *affectingKey = [<$Relationship.immutableCollectionClassName$> setWithObject:@"<$Attribute.name$>"];
keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKey];
}<$endif$><$endif$><$endforeach do$>

Expand Down Expand Up @@ -69,9 +69,9 @@
@dynamic <$Relationship.name$>;

<$if Relationship.isToMany$>
- (NSMutableSet*)<$Relationship.name$>Set {
- (<$Relationship.mutableCollectionClassName$>*)<$Relationship.name$>Set {
[self willAccessValueForKey:@"<$Relationship.name$>"];
NSMutableSet *result = [self mutableSetValueForKey:@"<$Relationship.name$>"];
<$Relationship.mutableCollectionClassName$> *result = [self mutableSetValueForKey:@"<$Relationship.name$>"];
[self didAccessValueForKey:@"<$Relationship.name$>"];
return result;
}
Expand Down

0 comments on commit 4e9a045

Please sign in to comment.