-
Notifications
You must be signed in to change notification settings - Fork 4
/
machine.m.motemplate
296 lines (261 loc) · 14.9 KB
/
machine.m.motemplate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// DO NOT EDIT. This file is machine-generated and constantly overwritten.
// Make changes to <$managedObjectClassName$>.m instead.
#import "_<$managedObjectClassName$>.h"<$foreach Relationship noninheritedRelationships do$>
#import "_<$Relationship.destinationEntity.managedObjectClassName$>.h"<$endforeach do$>
@implementation <$managedObjectClassName$>ID
@end
@implementation <$managedObjectClassName$>Attribute
<$foreach Attribute noninheritedAttributes do$>
- (QKAttribute *)<$Attribute.name$> {
return [[QKAttribute alloc] initWithAttributes:self, [_<$managedObjectClassName$> <$Attribute.name$>], nil];
}
<$endforeach do$>
<$foreach Relationship noninheritedRelationships do$>
- (<$Relationship.destinationEntity.managedObjectClassName$>Attribute *)<$Relationship.name$> {
return [[<$Relationship.destinationEntity.managedObjectClassName$>Attribute alloc] initWithAttributes:self, [_<$managedObjectClassName$> <$Relationship.name$>], nil];
}
<$endforeach do$>
@end
@implementation _<$managedObjectClassName$>
#pragma mark - Helpers
+ (QKQuerySet *)querySetWithManagedObjectContext:(NSManagedObjectContext *)context {
return [[QKQuerySet alloc] initWithManagedObjectContext:context entityDescription:[self entityInManagedObjectContext:context]];
}
+ (instancetype)insertInManagedObjectContext:(NSManagedObjectContext *)managedObjectContext {
NSParameterAssert(managedObjectContext);
return [NSEntityDescription insertNewObjectForEntityForName:@"<$name$>" inManagedObjectContext:managedObjectContext];
}
+ (NSString *)entityName {
return @"<$name$>";
}
+ (NSEntityDescription *)entityInManagedObjectContext:(NSManagedObjectContext *)managedObjectContext {
NSParameterAssert(managedObjectContext);
return [NSEntityDescription entityForName:@"<$name$>" inManagedObjectContext:managedObjectContext];
}
- (<$managedObjectClassName$>ID *)objectID {
return (<$managedObjectClassName$>ID *)[super objectID];
}
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
NSSet *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$>"];
keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKey];
return keyPaths;
}<$endif$><$endif$><$endforeach do$>
return keyPaths;
}
#pragma mark - Attributes
<$foreach Attribute noninheritedAttributes do$>
+ (QKAttribute *)<$Attribute.name$> {
return [[QKAttribute alloc] initWithName:@"<$Attribute.name$>"];
}
<$endforeach do$>
<$foreach Relationship noninheritedRelationships do$>
+ (QKAttribute *)<$Relationship.name$> {
return [[QKAttribute alloc] initWithName:@"<$Relationship.name$>"];
}
<$endforeach do$>
#pragma mark - Properties
<$foreach Attribute noninheritedAttributes do$>
<$if Attribute.hasDefinedAttributeType$>
@dynamic <$Attribute.name$>;
<$if Attribute.hasScalarAttributeType$>
- (<$Attribute.scalarAttributeType$>)<$Attribute.name$>Value {
return [self.<$Attribute.name$> <$Attribute.scalarAccessorMethodName$>];
}
<$if ! Attribute.isReadonly$>
- (void)set<$Attribute.name.initialCapitalString$>Value:(<$Attribute.scalarAttributeType$>)value {
self.<$Attribute.name$> = @(value);
}
<$endif$>
<$endif$>
<$endif$>
<$endforeach do$>
<$foreach Relationship noninheritedRelationships do$>
@dynamic <$Relationship.name$>;
<$if Relationship.isToMany$>
- (<$Relationship.mutableCollectionClassName$> *)<$Relationship.name$>Set {
[self willAccessValueForKey:@"<$Relationship.name$>"];
<$if Relationship.jr_isOrdered$>
<$Relationship.mutableCollectionClassName$> *result = (<$Relationship.mutableCollectionClassName$> *)[self mutableOrderedSetValueForKey:@"<$Relationship.name$>"];
<$else$>
<$Relationship.mutableCollectionClassName$> *result = (<$Relationship.mutableCollectionClassName$> *)[self mutableSetValueForKey:@"<$Relationship.name$>"];
<$endif$>
[self didAccessValueForKey:@"<$Relationship.name$>"];
return result;
}
<$endif$>
<$endforeach do$>
<$foreach FetchedProperty noninheritedFetchedProperties do$>
@dynamic <$FetchedProperty.name$>;
<$endforeach do$>
<$foreach FetchRequest prettyFetchRequests do$>
<$if FetchRequest.singleResult$>
+ (id)fetch<$FetchRequest.name.initialCapitalString$>:(NSManagedObjectContext *)moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:(<$Binding.type$>)<$Binding.name$>_ <$endforeach do2$>{
NSError *error = nil;
id result = [self fetch<$FetchRequest.name.initialCapitalString$>:moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:<$Binding.name$>_ <$endforeach do2$>error:&error];
if (error) {
#ifdef NSAppKitVersionNumber10_0
[NSApp presentError:error];
#else
NSLog(@"error: %@", error);
#endif
}
return result;
}
+ (id)fetch<$FetchRequest.name.initialCapitalString$>:(NSManagedObjectContext *)managedObjectContext <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:(<$Binding.type$>)<$Binding.name$>_ <$endforeach do2$>error:(NSError **)error_ {
NSParameterAssert(moc_);
NSError *error = nil;
NSManagedObjectModel *model = [[moc_ persistentStoreCoordinator] managedObjectModel];
<$if FetchRequest.hasBindings$>
NSDictionary *substitutionVariables = [NSDictionary dictionaryWithObjectsAndKeys:
<$foreach Binding FetchRequest.bindings do2$>
<$Binding.name$>_, @"<$Binding.name$>",
<$endforeach do2$>
nil];
<$else$>
NSDictionary *substitutionVariables = [NSDictionary dictionary];
<$endif$>
NSFetchRequest *fetchRequest = [model fetchRequestFromTemplateWithName:@"<$FetchRequest.name$>"
substitutionVariables:substitutionVariables];
NSAssert(fetchRequest, @"Can't find fetch request named \"<$FetchRequest.name$>\".");
id result = nil;
NSArray *results = [moc_ executeFetchRequest:fetchRequest error:&error];
if (!error) {
switch ([results count]) {
case 0:
// Nothing found matching the fetch request. That's cool, though: we'll just return nil.
break;
case 1:
result = [results objectAtIndex:0];
break;
default:
NSLog(@"WARN fetch request <$FetchRequest.name$>: 0 or 1 objects expected, %lu found (substitutionVariables:%@, results:%@)",
(unsigned long)[results count],
substitutionVariables,
results);
}
}
if (error_) *error_ = error;
return result;
}
<$else$>
+ (NSArray *)fetch<$FetchRequest.name.initialCapitalString$>:(NSManagedObjectContext *)moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:(<$Binding.type$>)<$Binding.name$>_ <$endforeach do2$>{
NSError *error = nil;
NSArray *result = [self fetch<$FetchRequest.name.initialCapitalString$>:moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:<$Binding.name$>_ <$endforeach do2$>error:&error];
if (error) {
#ifdef NSAppKitVersionNumber10_0
[NSApp presentError:error];
#else
NSLog(@"error: %@", error);
#endif
}
return result;
}
+ (NSArray *)fetch<$FetchRequest.name.initialCapitalString$>:(NSManagedObjectContext *)moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:(<$Binding.type$>)<$Binding.name$>_ <$endforeach do2$>error:(NSError **)error_ {
NSParameterAssert(moc_);
NSError *error = nil;
NSManagedObjectModel *model = [[moc_ persistentStoreCoordinator] managedObjectModel];
<$if FetchRequest.hasBindings$>
NSDictionary *substitutionVariables = [NSDictionary dictionaryWithObjectsAndKeys:
<$foreach Binding FetchRequest.bindings do2$>
<$Binding.name$>_, @"<$Binding.name$>",
<$endforeach do2$>
nil];
<$else$>
NSDictionary *substitutionVariables = [NSDictionary dictionary];
<$endif$>
NSFetchRequest *fetchRequest = [model fetchRequestFromTemplateWithName:@"<$FetchRequest.name$>"
substitutionVariables:substitutionVariables];
NSAssert(fetchRequest, @"Can't find fetch request named \"<$FetchRequest.name$>\".");
NSArray *result = [moc_ executeFetchRequest:fetchRequest error:&error];
if (error_) *error_ = error;
return result;
}
<$endif$>
<$endforeach do$>
<$if TemplateVar.frc$>
#if TARGET_OS_IPHONE
<$foreach Relationship noninheritedRelationships do$>
<$if Relationship.isToMany$>
- (NSFetchedResultsController *)new<$Relationship.name.initialCapitalString$>FetchedResultsControllerWithSortDescriptors:(NSArray *)sortDescriptors {
NSFetchRequest *fetchRequest = [NSFetchRequest new];
fetchRequest.entity = [NSEntityDescription entityForName:@"<$Relationship.destinationEntity.name$>" inManagedObjectContext:self.managedObjectContext];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"<$Relationship.inverseRelationship.name$> <$if Relationship.inverseRelationship.isToMany$>CONTAINS<$else$>==<$endif$> %@", self];
fetchRequest.sortDescriptors = sortDescriptors;
<$if indexedNoninheritedAttributes.@count > 0$>
NSArray *indexedIDs = [NSArray arrayWithObjects:<$foreach Attribute indexedNoninheritedAttributes do$>self.<$Attribute.name$>, <$endforeach do$>nil];
NSString *cacheName = [NSString stringWithFormat:@"mogenerator.<$managedObjectClassName$>.%@.<$Relationship.name$>.%@", indexedIDs, sortDescriptors];
<$endif$>
return [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:<$if indexedNoninheritedAttributes.@count > 0$>cacheName<$else$>nil<$endif$>];
}
<$endif$>
<$endforeach do$>
#endif
<$endif$>
@end
<$foreach Relationship noninheritedRelationships do$><$if Relationship.isToMany$><$if Relationship.jr_isOrdered$>
@implementation _<$managedObjectClassName$> (<$Relationship.name.initialCapitalString$>CoreDataGeneratedAccessors)
- (void)add<$Relationship.name.initialCapitalString$>:(<$Relationship.immutableCollectionClassName$> *)value_ {
[self.<$Relationship.name$>Set unionOrderedSet:value_];
}
- (void)remove<$Relationship.name.initialCapitalString$>:(<$Relationship.immutableCollectionClassName$> *)value_ {
[self.<$Relationship.name$>Set minusOrderedSet:value_];
}
- (void)add<$Relationship.name.initialCapitalString$>Object:(<$Relationship.destinationEntity.managedObjectClassName$> *)value_ {
[self.<$Relationship.name$>Set addObject:value_];
}
- (void)remove<$Relationship.name.initialCapitalString$>Object:(<$Relationship.destinationEntity.managedObjectClassName$> *)value_ {
[self.<$Relationship.name$>Set removeObject:value_];
}
- (void)insertObject:(<$Relationship.destinationEntity.managedObjectClassName$> *)value in<$Relationship.name.initialCapitalString$>AtIndex:(NSUInteger)idx {
NSIndexSet *indexes = [NSIndexSet indexSetWithIndex:idx];
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self <$Relationship.name$>]];
[tmpOrderedSet insertObject:value atIndex:idx];
[self setPrimitiveValue:tmpOrderedSet forKey:@"<$Relationship.name$>"];
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
}
- (void)removeObjectFrom<$Relationship.name.initialCapitalString$>AtIndex:(NSUInteger)idx {
NSIndexSet *indexes = [NSIndexSet indexSetWithIndex:idx];
[self willChange:NSKeyValueChangeRemoval valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self <$Relationship.name$>]];
[tmpOrderedSet removeObjectAtIndex:idx];
[self setPrimitiveValue:tmpOrderedSet forKey:@"<$Relationship.name$>"];
[self didChange:NSKeyValueChangeRemoval valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
}
- (void)insert<$Relationship.name.initialCapitalString$>:(NSArray *)value atIndexes:(NSIndexSet *)indexes {
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self <$Relationship.name$>]];
[tmpOrderedSet insertObjects:value 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 <$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)idx withObject:(<$Relationship.destinationEntity.managedObjectClassName$> *)value {
NSIndexSet *indexes = [NSIndexSet indexSetWithIndex:idx];
[self willChange:NSKeyValueChangeReplacement valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self <$Relationship.name$>]];
[tmpOrderedSet replaceObjectAtIndex:idx 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 *)value {
[self willChange:NSKeyValueChangeReplacement valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self <$Relationship.name$>]];
[tmpOrderedSet replaceObjectsAtIndexes:indexes withObjects:value];
[self setPrimitiveValue:tmpOrderedSet forKey:@"<$Relationship.name$>"];
[self didChange:NSKeyValueChangeReplacement valuesAtIndexes:indexes forKey:@"<$Relationship.name$>"];
}
@end
<$endif$><$endif$><$endforeach do$>