mogenerator.templates/machine.swift.motemplate
author Xavier Algarra <xavier@pep-project.org>
Tue, 06 Jun 2017 07:49:23 +0200
changeset 2248 75013a586bfd
parent 787 55b0b69a4288
permissions -rw-r--r--
IOS-137 add multiple accounts option with the new login
dirk@475
     1
// DO NOT EDIT. This file is machine-generated and constantly overwritten.
dirk@475
     2
// Make changes to <$sanitizedManagedObjectClassName$>.swift instead.
dirk@475
     3
dirk@475
     4
import Foundation
dirk@475
     5
import CoreData
dirk@475
     6
<$if hasCustomBaseCaseImport$>import <$baseClassImport$><$endif$>
dirk@475
     7
dirk@475
     8
<$if noninheritedAttributes.@count > 0$>
dirk@475
     9
public enum <$sanitizedManagedObjectClassName$>Attributes: String {<$foreach Attribute noninheritedAttributes do$>
dirk@475
    10
    case <$Attribute.name$> = "<$Attribute.name$>"<$endforeach do$>
dirk@475
    11
}
dirk@475
    12
<$endif$>
dirk@475
    13
dirk@475
    14
<$if noninheritedRelationships.@count > 0$>
dirk@475
    15
public enum <$sanitizedManagedObjectClassName$>Relationships: String {<$foreach Relationship noninheritedRelationships do$>
dirk@475
    16
    case <$Relationship.name$> = "<$Relationship.name$>"<$endforeach do$>
dirk@475
    17
}
dirk@475
    18
<$endif$>
dirk@475
    19
dirk@475
    20
<$if noninheritedFetchedProperties.@count > 0$>
dirk@475
    21
public enum <$sanitizedManagedObjectClassName$>FetchedProperties: String {<$foreach FetchedProperty noninheritedFetchedProperties do$>
dirk@475
    22
    case <$FetchedProperty.name$> = "<$FetchedProperty.name$>"<$endforeach do$>
dirk@475
    23
}
dirk@475
    24
<$endif$>
dirk@475
    25
dirk@785
    26
<$if hasUserInfoKeys && userInfoKeyValues.@count > 0$>		
dirk@785
    27
public enum <$sanitizedManagedObjectClassName$>UserInfo: String {<$foreach UserInfo userInfoKeyValues do$>		
dirk@785
    28
    case <$UserInfo.key$> = "<$UserInfo.key$>"<$endforeach do$>		
dirk@785
    29
}		
dirk@475
    30
<$endif$>
dirk@475
    31
dirk@475
    32
dirk@475
    33
<$if hasCustomSuperentity $>
dirk@785
    34
open class _<$sanitizedManagedObjectClassName$>: <$customSuperentity$> {
dirk@475
    35
<$else$>
dirk@785
    36
open class _<$sanitizedManagedObjectClassName$>: NSManagedObject {
dirk@475
    37
<$endif$>
dirk@475
    38
    // MARK: - Class methods
dirk@475
    39
dirk@785
    40
    <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>open class func entityName () -> String {
dirk@475
    41
        return "<$name$>"
dirk@475
    42
    }
dirk@475
    43
dirk@785
    44
    <$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>open class func entity(managedObjectContext: NSManagedObjectContext) -> NSEntityDescription? {
dirk@785
    45
        return NSEntityDescription.entity(forEntityName: self.entityName(), in: managedObjectContext)
dirk@475
    46
    }
dirk@475
    47
dirk@475
    48
    // MARK: - Life cycle methods
dirk@475
    49
dirk@785
    50
    public override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
dirk@785
    51
        super.init(entity: entity, insertInto: context)
dirk@475
    52
    }
dirk@475
    53
dirk@475
    54
    public convenience init?(managedObjectContext: NSManagedObjectContext) {
dirk@785
    55
        guard let entity = _<$sanitizedManagedObjectClassName$>.entity(managedObjectContext: managedObjectContext) else { return nil }
dirk@785
    56
        self.init(entity: entity, insertInto: managedObjectContext)
dirk@475
    57
    }
dirk@475
    58
dirk@475
    59
    // MARK: - Properties
dirk@475
    60
<$foreach Attribute noninheritedAttributes do$>
dirk@475
    61
<$if Attribute.hasDefinedAttributeType$>
dirk@475
    62
<$if Attribute.hasScalarAttributeType$>
dirk@475
    63
<$if Attribute.isReadonly$>
dirk@787
    64
    open var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>
dirk@475
    65
    {
dirk@475
    66
        self.willAccessValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
dirk@475
    67
        let <$Attribute.name$> = self.primitiveValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? NSNumber
dirk@475
    68
        self.didAccessValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
dirk@475
    69
        return <$Attribute.name$>
dirk@475
    70
    }
dirk@475
    71
<$else$>
dirk@785
    72
    @NSManaged open
dirk@787
    73
    var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>
dirk@475
    74
<$endif$>
dirk@475
    75
<$else$>
dirk@475
    76
<$if Attribute.isReadonly$>
dirk@785
    77
    open var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>
dirk@475
    78
    {
dirk@475
    79
        self.willAccessValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
dirk@475
    80
        let <$Attribute.name$> = self.primitiveValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? <$Attribute.objectAttributeType$>
dirk@475
    81
        self.didAccessValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue)
dirk@475
    82
        return <$Attribute.name$>
dirk@475
    83
    }
dirk@475
    84
<$else$>
dirk@785
    85
    @NSManaged open
dirk@475
    86
    var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$>
dirk@475
    87
<$endif$>
dirk@475
    88
<$endif$>
dirk@475
    89
<$endif$>
dirk@475
    90
<$endforeach do$>
dirk@475
    91
dirk@475
    92
    // MARK: - Relationships
dirk@475
    93
<$foreach Relationship noninheritedRelationships do$>
dirk@475
    94
<$if Relationship.isToMany$>
dirk@785
    95
    @NSManaged open
dirk@475
    96
    var <$Relationship.name$>: <$Relationship.immutableCollectionClassName$>
dirk@475
    97
dirk@785
    98
    open func <$Relationship.name$>Set() -> <$Relationship.mutableCollectionClassName$> {
dirk@785
    99
        return self.<$Relationship.name$>.mutableCopy() as! <$Relationship.mutableCollectionClassName$>
dirk@785
   100
    }
dirk@785
   101
dirk@475
   102
<$else$>
dirk@785
   103
    @NSManaged open
dirk@475
   104
    var <$Relationship.name$>: <$Relationship.destinationEntity.sanitizedManagedObjectClassName$><$if Relationship.isOptional$>?<$endif$>
dirk@475
   105
<$endif$>
dirk@475
   106
<$endforeach do$>
dirk@475
   107
dirk@475
   108
<$foreach FetchRequest prettyFetchRequests do$>
dirk@475
   109
<$if FetchRequest.singleResult$>
dirk@475
   110
    class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>) -> AnyObject? {
dirk@475
   111
        return self.fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.name$><$endforeach do2$>, error: nil)
dirk@475
   112
    }
dirk@475
   113
dirk@475
   114
    class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>, error outError: NSErrorPointer) -> AnyObject? {
dirk@475
   115
        guard let psc = managedObjectContext.persistentStoreCoordinator else { return nil }
dirk@475
   116
        let model = psc.managedObjectModel
dirk@475
   117
        let substitutionVariables = [<$if FetchRequest.hasBindings$><$foreach Binding FetchRequest.bindings do2$>
dirk@475
   118
            "<$Binding.name$>": <$Binding.name$>,
dirk@475
   119
        <$endforeach do2$><$endif$>
dirk@475
   120
        ]
dirk@475
   121
dirk@475
   122
        guard let fetchRequest = model.fetchRequestFromTemplateWithName("<$FetchRequest.name$>", substitutionVariables: substitutionVariables) else {
dirk@475
   123
          assert(false, "Can't find fetch request named \"<$FetchRequest.name$>\".")
dirk@475
   124
          return nil
dirk@475
   125
        }
dirk@475
   126
dirk@475
   127
        var result: AnyObject? = nil
dirk@475
   128
        do {
dirk@475
   129
            let results = try managedObjectContext.executeFetchRequest(fetchRequest)
dirk@475
   130
            switch results.count {
dirk@475
   131
            case 0:
dirk@475
   132
                // Nothing found matching the fetch request. That's cool, though: we'll just return nil.
dirk@475
   133
                break
dirk@475
   134
            case 1:
dirk@475
   135
                result = results.first
dirk@475
   136
            default:
dirk@475
   137
                print("WARN fetch request <$FetchRequest.name$>: 0 or 1 objects expected, \(results.count) found (substitutionVariables: \(substitutionVariables), results: \(results))")
dirk@475
   138
            }
dirk@475
   139
dirk@475
   140
        } catch {
dirk@475
   141
            print("Error executing fetch request: \(error)")
dirk@475
   142
        }
dirk@475
   143
        return result
dirk@475
   144
    }
dirk@475
   145
<$else$>
dirk@475
   146
    class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>) -> [AnyObject]? {
dirk@475
   147
        return self.fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.name$><$endforeach do2$>, error: nil)
dirk@475
   148
    }
dirk@475
   149
dirk@475
   150
    class func fetch<$FetchRequest.name.initialCapitalString$>(managedObjectContext: NSManagedObjectContext<$foreach Binding FetchRequest.bindings do2$>, <$Binding.name$>: <$Binding.type$><$endforeach do2$>, error outError: NSErrorPointer) -> [AnyObject]? {
dirk@475
   151
        guard let psc = managedObjectContext.persistentStoreCoordinator else { return nil }
dirk@475
   152
        let model = psc.managedObjectModel
dirk@475
   153
        let substitutionVariables : [String : AnyObject] = [<$if FetchRequest.hasBindings$><$foreach Binding FetchRequest.bindings do2$>
dirk@475
   154
            "<$Binding.name$>": <$Binding.name$>,
dirk@475
   155
<$endforeach do2$><$else$>:<$endif$>]
dirk@475
   156
dirk@475
   157
        let fetchRequest = model.fetchRequestFromTemplateWithName("<$FetchRequest.name$>", substitutionVariables: substitutionVariables)
dirk@475
   158
        assert(fetchRequest != nil, "Can't find fetch request named \"<$FetchRequest.name$>\".")
dirk@475
   159
dirk@475
   160
        var results = Array<AnyObject>()
dirk@475
   161
        do {
dirk@475
   162
          results = try managedObjectContext.executeFetchRequest(fetchRequest!)
dirk@475
   163
        } catch {
dirk@475
   164
          print("Error executing fetch request: \(error)")
dirk@475
   165
        }
dirk@475
   166
dirk@475
   167
        return results
dirk@475
   168
    }
dirk@475
   169
<$endif$>
dirk@475
   170
<$endforeach do$>
dirk@475
   171
dirk@475
   172
<$foreach FetchedProperty noninheritedFetchedProperties do$>
dirk@785
   173
    @NSManaged open
dirk@475
   174
    let <$FetchedProperty.name$>: [<$FetchedProperty.entity.sanitizedManagedObjectClassName$>]
dirk@475
   175
<$endforeach do$>
dirk@475
   176
}
dirk@475
   177
dirk@475
   178
<$foreach Relationship noninheritedRelationships do$><$if Relationship.isToMany$>
dirk@785
   179
extension _<$sanitizedManagedObjectClassName$> {
dirk@475
   180
dirk@785
   181
    open func add<$Relationship.name.initialCapitalString$>(objects: <$Relationship.immutableCollectionClassName$>) {
dirk@475
   182
        let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
dirk@785
   183
        mutable.union(objects<$if !Relationship.jr_isOrdered$> as Set<NSObject><$endif$>)
dirk@475
   184
        self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
dirk@475
   185
    }
dirk@475
   186
dirk@785
   187
    open func remove<$Relationship.name.initialCapitalString$>(objects: <$Relationship.immutableCollectionClassName$>) {
dirk@475
   188
        let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
dirk@785
   189
        mutable.minus(objects<$if !Relationship.jr_isOrdered$> as Set<NSObject><$endif$>)
dirk@475
   190
        self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
dirk@475
   191
    }
dirk@475
   192
dirk@785
   193
    open func add<$Relationship.name.initialCapitalString$>Object(value: <$Relationship.destinationEntity.sanitizedManagedObjectClassName$>) {
dirk@475
   194
        let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
dirk@785
   195
        mutable.add(value)
dirk@475
   196
        self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
dirk@475
   197
    }
dirk@475
   198
dirk@785
   199
    open func remove<$Relationship.name.initialCapitalString$>Object(value: <$Relationship.destinationEntity.sanitizedManagedObjectClassName$>) {
dirk@475
   200
        let mutable = self.<$Relationship.name$>.mutableCopy() as! NSMutable<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
dirk@785
   201
        mutable.remove(value)
dirk@475
   202
        self.<$Relationship.name$> = mutable.copy() as! NS<$if Relationship.jr_isOrdered$>Ordered<$endif$>Set
dirk@475
   203
    }
dirk@475
   204
dirk@475
   205
}
dirk@475
   206
<$endif$><$endforeach do$>