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