-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine.swift.motemplate
190 lines (156 loc) · 8.81 KB
/
machine.swift.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
// DO NOT EDIT. This file is machine-generated and constantly overwritten.
// Make changes to <$managedObjectClassName$>.swift instead.
import CoreData
<$if noninheritedAttributes.@count > 0$>
enum <$managedObjectClassName$>Attributes: String {<$foreach Attribute noninheritedAttributes do$>
case <$Attribute.name$> = "<$Attribute.name$>"<$endforeach do$>
}
<$endif$>
<$if noninheritedRelationships.@count > 0$>
enum <$managedObjectClassName$>Relationships: String {<$foreach Relationship noninheritedRelationships do$>
case <$Relationship.name$> = "<$Relationship.name$>"<$endforeach do$>
}
<$endif$>
<$if noninheritedFetchedProperties.@count > 0$>
enum <$managedObjectClassName$>FetchedProperties: String {<$foreach FetchedProperty noninheritedFetchedProperties do$>
case <$FetchedProperty.name$> = "<$FetchedProperty.name$>"<$endforeach do$>
}
<$endif$>
<$if hasUserInfoKeys$>
enum <$managedObjectClassName$>UserInfo: String {<$foreach UserInfo userInfoKeyValues do$>
case <$UserInfo.key$> = "<$UserInfo.key$>"<$endforeach do$>
}
<$endif$>
@objc
public class _<$managedObjectClassName$>: <$customSuperentity$> {
// MARK: - Class methods
<$if hasCustomSuperentity$>override <$endif$>class func entityName () -> String {
return "<$name$>"
}
<$if hasCustomSuperentity$>override <$endif$>class func entity(managedObjectContext: NSManagedObjectContext!) -> NSEntityDescription! {
return NSEntityDescription.entityForName(self.entityName(), inManagedObjectContext: managedObjectContext);
}
// MARK: - Life cycle methods
override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext!) {
super.init(entity: entity, insertIntoManagedObjectContext: context)
}
convenience init(managedObjectContext: NSManagedObjectContext!) {
let entity = _<$managedObjectClassName$>.entity(managedObjectContext)
self.init(entity: entity, insertIntoManagedObjectContext: managedObjectContext)
}
// MARK: - Properties
<$foreach Attribute noninheritedAttributes do$>
<$if Attribute.hasDefinedAttributeType$>
<$if Attribute.hasScalarAttributeType$>
<$if Attribute.isReadonly$>
@NSManaged
public let <$Attribute.name$>: NSNumber?
<$else$>
@NSManaged
public var <$Attribute.name$>: NSNumber?
<$endif$>
<$else$>
<$if Attribute.isReadonly$>
@NSManaged
public let <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>
<$else$>
@NSManaged
public var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>
<$endif$>
<$endif$>
// func validate<$Attribute.name.initialCapitalString$>(value: AutoreleasingUnsafePointer<AnyObject>, error: NSErrorPointer) {}
<$endif$>
<$endforeach do$>
// MARK: - Relationships
<$foreach Relationship noninheritedRelationships do$>
<$if Relationship.isToMany$>
@NSManaged
public var <$Relationship.name$>: <$Relationship.immutableCollectionClassName$>
<$else$>
@NSManaged
public var <$Relationship.name$>: <$Relationship.destinationEntity.managedObjectClassName$><$if Relationship.isOptional$>?<$endif$>
// func validate<$Relationship.name.initialCapitalString$>(value: AutoreleasingUnsafePointer<AnyObject>, error: NSErrorPointer) {}
<$endif$>
<$endforeach do$>
<$foreach FetchRequest prettyFetchRequests do$>
<$if FetchRequest.singleResult$>
class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext!<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>) -> AnyObject? {
return self.fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.name$><$endforeach do2$>, error: nil)
}
class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext!<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>, error outError: NSErrorPointer) -> AnyObject? {
let model = managedObjectContext.persistentStoreCoordinator().managedObjectModel()
let substitutionVariables = [<$if FetchRequest.hasBindings$><$foreach Binding FetchRequest.bindings do2$>
"<$Binding.name$>": <$Binding.name$>,
<$endforeach do2$><$endif$> ]
let fetchRequest = model.fetchRequestFromTemplateWithName("<$FetchRequest.name$>", substitutionVariables: substitutionVariables)
assert(fetchRequest != nil, "Can't find fetch request named \"<$FetchRequest.name$>\".")
var error: NSError? = nil
var result: AnyObject? = nil
let results = managedObjectContext.executeFetchRequest(fetchRequest, error: &error)
if error != nil {
outError.memory = error
} else {
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[0]
default:
NSLog("WARN fetch request <$FetchRequest.name$>: 0 or 1 objects expected, %lu found (substitutionVariables:%@, results:%@)",
results.count,
substitutionVariables,
results);
}
}
return result;
}
<$else$>
class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext!<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>) -> [AnyObject] {
return self.fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.name$><$endforeach do2$>, error: nil)
}
class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext!<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>, error outError: NSErrorPointer) -> [AnyObject] {
let model = managedObjectContext.persistentStoreCoordinator.managedObjectModel
let substitutionVariables = [<$if FetchRequest.hasBindings$><$foreach Binding FetchRequest.bindings do2$>
"<$Binding.name$>": <$Binding.name$>,
<$endforeach do2$><$else$>:<$endif$>]
let fetchRequest = model.fetchRequestFromTemplateWithName("<$FetchRequest.name$>", substitutionVariables: substitutionVariables)
assert(fetchRequest != nil, "Can't find fetch request named \"<$FetchRequest.name$>\".")
var error: NSError? = nil
let results = managedObjectContext.executeFetchRequest(fetchRequest, error: &error)
if error != nil {
outError.memory = error
}
return results
}
<$endif$>
<$endforeach do$>
<$foreach FetchedProperty noninheritedFetchedProperties do$>
@NSManaged
let <$FetchedProperty.name$>: [<$FetchedProperty.entity.managedObjectClassName$>]
<$endforeach do$>
}
<$foreach Relationship noninheritedRelationships do$><$if Relationship.isToMany$>
extension _<$managedObjectClassName$> {
func add<$Relationship.name.initialCapitalString$>(objects: <$Relationship.immutableCollectionClassName$>) {
let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
mutable.union<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set(objects as! Set<NSObject>)
self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
}
func remove<$Relationship.name.initialCapitalString$>(objects: <$Relationship.immutableCollectionClassName$>) {
let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
mutable.minus<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set(objects as! Set<NSObject>)
self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
}
func add<$Relationship.name.initialCapitalString$>Object(value: <$Relationship.destinationEntity.managedObjectClassName$>!) {
let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
mutable.addObject(value)
self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
}
func remove<$Relationship.name.initialCapitalString$>Object(value: <$Relationship.destinationEntity.managedObjectClassName$>!) {
let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
mutable.removeObject(value)
self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
}
}
<$endif$><$endforeach do$>