From 4ccb3dc84e899a81202367f7f86996c24cbb260b Mon Sep 17 00:00:00 2001 From: Dmitry Makarenko Date: Thu, 3 Jan 2013 16:14:52 +0000 Subject: [PATCH] Templates were updated to generate CoreDataGeneratedAccessors methods' prototypes/bodies specifically for ordered relationships (issue #140). --- templates/machine.h.motemplate | 13 ++++ templates/machine.m.motemplate | 125 +++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/templates/machine.h.motemplate b/templates/machine.h.motemplate index 33b2e61c..4cf6f932 100644 --- a/templates/machine.h.motemplate +++ b/templates/machine.h.motemplate @@ -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 diff --git a/templates/machine.m.motemplate b/templates/machine.m.motemplate index ba6723a8..72b81568 100644 --- a/templates/machine.m.motemplate +++ b/templates/machine.m.motemplate @@ -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$> }; @@ -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