Skip to content

Commit

Permalink
Templates were updated to generate CoreDataGeneratedAccessors methods…
Browse files Browse the repository at this point in the history
…' prototypes/bodies specifically for ordered relationships (issue rentzsch#140).
  • Loading branch information
Dmitry Makarenko committed Jan 3, 2013
1 parent fbfcaaa commit 4ccb3dc
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
13 changes: 13 additions & 0 deletions templates/machine.h.motemplate
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,23 @@ extern const struct <$managedObjectClassName$>FetchedProperties {<$foreach Fetch

@interface _<$managedObjectClassName$> (CoreDataGeneratedAccessors)
<$foreach Relationship noninheritedRelationships do$><$if Relationship.isToMany$>
<$if !Relationship.jr_isOrdered$>
- (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_;
<$else$>
- (void)insertObject:(<$Relationship.destinationEntity.managedObjectClassName$>*)value_ in<$Relationship.name.initialCapitalString$>AtIndex:(NSUInteger)index_;
- (void)removeObjectFrom<$Relationship.name.initialCapitalString$>AtIndex:(NSUInteger)index_;
- (void)insert<$Relationship.name.initialCapitalString$>:(NSArray*)values_ atIndexes:(NSIndexSet*)indexes_;
- (void)remove<$Relationship.name.initialCapitalString$>AtIndexes:(NSIndexSet*)indexes_;
- (void)replaceObjectIn<$Relationship.name.initialCapitalString$>AtIndex:(NSUInteger)index_ withObject:(<$Relationship.destinationEntity.managedObjectClassName$>*)value_;
- (void)replace<$Relationship.name.initialCapitalString$>AtIndexes:(NSIndexSet*)indexes_ with<$Relationship.name.initialCapitalString$>:(NSArray*)values_;
- (void)add<$Relationship.name.initialCapitalString$>Object:(<$Relationship.destinationEntity.managedObjectClassName$>*)value_;
- (void)remove<$Relationship.name.initialCapitalString$>Object:(<$Relationship.destinationEntity.managedObjectClassName$>*)value_;
- (void)add<$Relationship.name.initialCapitalString$>:(<$Relationship.immutableCollectionClassName$>*)values_;
- (void)remove<$Relationship.name.initialCapitalString$>:(<$Relationship.immutableCollectionClassName$>*)values_;
<$endif$>
<$endif$><$endforeach do$>
@end

Expand Down
125 changes: 125 additions & 0 deletions templates/machine.m.motemplate
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@

#import "_<$managedObjectClassName$>.h"

// Imports necessary for ordered relationships CoreDataGeneratedAccessors
// ----------------------------------------------------------------------------
<$foreach Relationship noninheritedRelationships do$>
<$if Relationship.isToMany$>
<$if Relationship.jr_isOrdered$>
#import "<$Relationship.destinationEntity.managedObjectClassName$>.h"
<$endif$>
<$endif$>
<$endforeach do$>
// ----------------------------------------------------------------------------

const struct <$managedObjectClassName$>Attributes <$managedObjectClassName$>Attributes = {<$foreach Attribute noninheritedAttributes do$>
.<$Attribute.name$> = @"<$Attribute.name$>",<$endforeach do$>
};
Expand Down Expand Up @@ -214,3 +225,117 @@ const struct <$managedObjectClassName$>FetchedProperties <$managedObjectClassNam
#endif
<$endif$>
@end

@implementation _<$managedObjectClassName$> (CoreDataGeneratedAccessors)
<$foreach Relationship noninheritedRelationships do$><$if Relationship.isToMany$>
<$if Relationship.jr_isOrdered$>

- (void)insertObject:(<$Relationship.destinationEntity.managedObjectClassName$>*)value_ in<$Relationship.name.initialCapitalString$>AtIndex:(NSUInteger)index_ {
NSIndexSet *indexes = [NSIndexSet indexSetWithIndex:index_];
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self mutableOrderedSetValueForKey:@"<$Relationship.name$>"]];
[tmpOrderedSet insertObject:value_ atIndex:index_];
[self setPrimitiveValue:tmpOrderedSet forKey:@"<$Relationship.name$>"];
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
}

- (void)removeObjectFrom<$Relationship.name.initialCapitalString$>AtIndex:(NSUInteger)index_ {
NSIndexSet *indexes = [NSIndexSet indexSetWithIndex:index_];
[self willChange:NSKeyValueChangeRemoval valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self mutableOrderedSetValueForKey:@"<$Relationship.name$>"]];
[tmpOrderedSet removeObjectAtIndex:index_];
[self setPrimitiveValue:tmpOrderedSet forKey:@"<$Relationship.name$>"];
[self didChange:NSKeyValueChangeRemoval valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
}

- (void)insert<$Relationship.name.initialCapitalString$>:(NSArray*)values_ atIndexes:(NSIndexSet*)indexes_ {
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes_ forKey:@"<$Relationship.name$>"];
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self mutableOrderedSetValueForKey:@"<$Relationship.name$>"]];
[tmpOrderedSet insertObjects:values_ atIndexes:indexes_];
[self setPrimitiveValue:tmpOrderedSet forKey:@"<$Relationship.name$>"];
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes_ forKey:@"<$Relationship.name$>"];
}

- (void)remove<$Relationship.name.initialCapitalString$>AtIndexes:(NSIndexSet*)indexes_ {
[self willChange:NSKeyValueChangeRemoval valuesAtIndexes:indexes_ forKey:@"<$Relationship.name$>"];
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self mutableOrderedSetValueForKey:@"<$Relationship.name$>"]];
[tmpOrderedSet removeObjectsAtIndexes:indexes_];
[self setPrimitiveValue:tmpOrderedSet forKey:@"<$Relationship.name$>"];
[self didChange:NSKeyValueChangeRemoval valuesAtIndexes:indexes_ forKey:@"<$Relationship.name$>"];
}

- (void)replaceObjectIn<$Relationship.name.initialCapitalString$>AtIndex:(NSUInteger)index_ withObject:(<$Relationship.destinationEntity.managedObjectClassName$>*)value_ {
NSIndexSet *indexes = [NSIndexSet indexSetWithIndex:index_];
[self willChange:NSKeyValueChangeReplacement valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self mutableOrderedSetValueForKey:@"<$Relationship.name$>"]];
[tmpOrderedSet replaceObjectAtIndex:index_ withObject:value_];
[self setPrimitiveValue:tmpOrderedSet forKey:@"<$Relationship.name$>"];
[self didChange:NSKeyValueChangeReplacement valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
}

- (void)replace<$Relationship.name.initialCapitalString$>AtIndexes:(NSIndexSet*)indexes_ with<$Relationship.name.initialCapitalString$>:(NSArray*)values_ {
[self willChange:NSKeyValueChangeReplacement valuesAtIndexes:indexes_ forKey:@"<$Relationship.name$>"];
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self mutableOrderedSetValueForKey:@"<$Relationship.name$>"]];
[tmpOrderedSet replaceObjectsAtIndexes:indexes_ withObjects:values_];
[self setPrimitiveValue:tmpOrderedSet forKey:@"<$Relationship.name$>"];
[self didChange:NSKeyValueChangeReplacement valuesAtIndexes:indexes_ forKey:@"<$Relationship.name$>"];
}

- (void)add<$Relationship.name.initialCapitalString$>Object:(<$Relationship.destinationEntity.managedObjectClassName$>*)value_ {
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self mutableOrderedSetValueForKey:@"<$Relationship.name$>"]];
NSUInteger index = [tmpOrderedSet count];
NSIndexSet *indexes = [NSIndexSet indexSetWithIndex:index];
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
[tmpOrderedSet addObject:value_];
[self setPrimitiveValue:tmpOrderedSet forKey:@"<$Relationship.name$>"];
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
}

- (void)remove<$Relationship.name.initialCapitalString$>Object:(<$Relationship.destinationEntity.managedObjectClassName$>*)value_ {
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self mutableOrderedSetValueForKey:@"<$Relationship.name$>"]];
NSUInteger index = [tmpOrderedSet indexOfObject:value_];
if (index != NSNotFound) {
NSIndexSet *indexes = [NSIndexSet indexSetWithIndex:index];
[self willChange:NSKeyValueChangeRemoval valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
[tmpOrderedSet removeObject:value_];
[self setPrimitiveValue:tmpOrderedSet forKey:@"<$Relationship.name$>"];
[self didChange:NSKeyValueChangeRemoval valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
}
}

- (void)add<$Relationship.name.initialCapitalString$>:(<$Relationship.immutableCollectionClassName$>*)values_ {
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self mutableOrderedSetValueForKey:@"<$Relationship.name$>"]];
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
NSUInteger valuesCount = [values_ count];
NSUInteger objectsCount = [tmpOrderedSet count];
for (NSUInteger i = 0; i < valuesCount; ++i) {
[indexes addIndex:(objectsCount + i)];
}
if (valuesCount > 0) {
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
[tmpOrderedSet addObjectsFromArray:[values_ array]];
[self setPrimitiveValue:tmpOrderedSet forKey:@"<$Relationship.name$>"];
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
}
}

- (void)remove<$Relationship.name.initialCapitalString$>:(<$Relationship.immutableCollectionClassName$>*)values_ {
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self mutableOrderedSetValueForKey:@"<$Relationship.name$>"]];
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
for (id value in values_) {
NSUInteger index = [tmpOrderedSet indexOfObject:value];
if (index != NSNotFound) {
[indexes addIndex:index];
}
}
if ([indexes count] > 0) {
[self willChange:NSKeyValueChangeRemoval valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
[tmpOrderedSet removeObjectsAtIndexes:indexes];
[self setPrimitiveValue:tmpOrderedSet forKey:@"<$Relationship.name$>"];
[self didChange:NSKeyValueChangeRemoval valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
}
}

<$endif$>
<$endif$><$endforeach do$>
@end

0 comments on commit 4ccb3dc

Please sign in to comment.