-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathCOCollection.m
More file actions
212 lines (169 loc) · 5.49 KB
/
Copy pathCOCollection.m
File metadata and controls
212 lines (169 loc) · 5.49 KB
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
/*
Copyright (C) 2011 Quentin Mathe
Date: December 2011
License: MIT (see COPYING)
*/
#import "COCollection.h"
#import "COEditingContext.h"
#import "COObjectGraphContext.h"
#import "COObject+Private.h"
#import "COPersistentRoot.h"
@implementation COCollection
+ (void)initialize
{
if (self != [COCollection class])
return;
[self applyTraitFromClass: [ETCollectionTrait class]];
[self applyTraitFromClass: [ETMutableCollectionTrait class]];
}
+ (ETPropertyDescription *)contentPropertyDescriptionWithName: (NSString *)aName
type: (NSString *)aType
opposite: (NSString *)oppositeType
{
ETPropertyDescription *contentProperty =
[ETPropertyDescription descriptionWithName: aName typeName: aType];
contentProperty.multivalued = YES;
contentProperty.opposite = (id)oppositeType;
contentProperty.ordered = YES;
contentProperty.persistent = YES;
return contentProperty;
}
+ (ETEntityDescription *)newEntityDescription
{
ETEntityDescription *collection = [self newBasicEntityDescription];
// For subclasses that don't override -newEntityDescription, we must not add the
// property descriptions that we will inherit through the parent
if (![collection.name isEqual: [COCollection className]])
return collection;
return collection;
}
- (instancetype)initWithObjectGraphContext: (COObjectGraphContext *)aContext
{
if ([[self class] isEqual: [COCollection class]])
{
[NSException raise: NSGenericException
format: @"Attempt to initialize abstract class %@", [self class]];
}
self = [super initWithObjectGraphContext: aContext];
if (self == nil)
return nil;
ETEntityDescription *coreObjectEntity =
[aContext.modelDescriptionRepository entityDescriptionForClass: [COObject class]];
// NOTE: COCollection is abstract, so subclasses uses either COObject or
// a COCollection subentity.
if (![self.entityDescription isEqual: coreObjectEntity]
&& [self.entityDescription propertyDescriptionForName: self.contentKey] == nil)
{
[NSException raise: NSInternalInconsistencyException
format: @"Found no property description for -contentKey %@", self.contentKey];
}
return self;
}
- (ETUTI *)objectType
{
ETPropertyDescription *propertyDesc =
[self.entityDescription propertyDescriptionForName: self.contentKey];
ETModelDescriptionRepository *repo = self.persistentRoot.editingContext.modelDescriptionRepository;
return [ETUTI typeWithClass: [repo classForEntityDescription: propertyDesc.type]];
}
- (void)addObjects: (NSArray *)anArray
{
for (id object in anArray)
{
[self addObject: object];
}
}
- (void)didLoadObjectGraph
{
[self didUpdate];
}
- (void)didUpdate
{
[[NSNotificationCenter defaultCenter]
postNotificationName: ETCollectionDidUpdateNotification object: self];
}
- (NSString *)contentKey
{
return @"objects";
}
- (BOOL)isOrdered
{
// TODO: If too slow, return the boolean directly.
return [self.entityDescription propertyDescriptionForName: self.contentKey].ordered;
}
- (id)content
{
return [self valueForVariableStorageKey: self.contentKey];
}
- (NSArray *)contentArray
{
return [[self valueForProperty: self.contentKey] contentArray];
}
- (void)insertObjects: (NSArray *)objects atIndexes: (NSIndexSet *)indexes hints: (NSArray *)hints
{
id collection = [self collectionForProperty: self.contentKey mutationIndexes: indexes];
[self willChangeValueForProperty: self.contentKey
atIndexes: indexes
withObjects: objects
mutationKind: ETCollectionMutationKindInsertion];
[collection insertObjects: objects atIndexes: indexes hints: hints];
[self didChangeValueForProperty: self.contentKey
atIndexes: indexes
withObjects: objects
mutationKind: ETCollectionMutationKindInsertion];
}
- (void)removeObjects: (NSArray *)objects atIndexes: (NSIndexSet *)indexes hints: (NSArray *)hints
{
id collection = [self collectionForProperty: self.contentKey mutationIndexes: indexes];
[self willChangeValueForProperty: self.contentKey
atIndexes: indexes
withObjects: objects
mutationKind: ETCollectionMutationKindRemoval];
[collection removeObjects: objects atIndexes: indexes hints: hints];
[self didChangeValueForProperty: self.contentKey
atIndexes: indexes
withObjects: objects
mutationKind: ETCollectionMutationKindRemoval];
}
- (id)objectForIdentifier: (NSString *)anId
{
for (id object in self.content)
{
if ([[object identifier] isEqualToString: anId])
{
return object;
}
}
return nil;
}
- (NSArray *)objectsMatchingPredicate: (NSPredicate *)aPredicate
{
NSMutableArray *result = [NSMutableArray array];
for (COObject *object in self.content)
{
if ([aPredicate evaluateWithObject: object])
{
[result addObject: object];
}
}
return result;
}
@end
@implementation COObject (COCollectionTypeQuerying)
- (BOOL)isGroup
{
return NO;
}
- (BOOL)isTag
{
return NO;
}
- (BOOL)isContainer
{
return NO;
}
- (BOOL)isLibrary
{
return NO;
}
@end