Created
September 26, 2016 08:57
-
-
Save Igor-Palaguta/2b600ba43549537acb80b998b3decb97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DO NOT EDIT. This file is machine-generated and constantly overwritten. | |
// Make changes to <$sanitizedManagedObjectClassName$>.swift instead. | |
import Foundation | |
import CoreData | |
<$if noninheritedAttributes.@count > 0 || noninheritedRelationships.@count > 0$> | |
enum <$sanitizedManagedObjectClassName$>Fields: String { | |
<$foreach Attribute noninheritedAttributes do$> | |
case <$Attribute.name$> = "<$Attribute.name$>"<$endforeach do$> | |
<$foreach Relationship noninheritedRelationships do$> | |
case <$Relationship.name$> = "<$Relationship.name$>"<$endforeach do$> | |
} | |
<$endif$> | |
<$if hasCustomSuperentity $> | |
public class _<$sanitizedManagedObjectClassName$>: <$customSuperentity$> { | |
<$else$> | |
public class _<$sanitizedManagedObjectClassName$>: NSManagedObject { | |
<$endif$> | |
<$if noninheritedAttributes.@count > 0 || noninheritedRelationships.@count > 0$> | |
class func keyPathsForFields(fields: [<$sanitizedManagedObjectClassName$>Fields]) -> NSSet { | |
return NSSet(array: fields.map { $0.rawValue }) | |
} | |
class func keyPathsForField(field: <$sanitizedManagedObjectClassName$>Fields) -> NSSet { | |
return NSSet(object: field.rawValue) | |
} | |
<$endif$> | |
// MARK: - Class methods | |
<$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class func entityName () -> String { | |
return "<$name$>" | |
} | |
<$if (hasCustomSuperclass || (hasCustomSuperentity && TemplateVar.overrideBaseClass))$>override <$endif$>public class func entity(managedObjectContext: NSManagedObjectContext) -> NSEntityDescription? { | |
return NSEntityDescription.entityForName(self.entityName(), inManagedObjectContext: managedObjectContext) | |
} | |
// MARK: - Life cycle methods | |
public override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) { | |
super.init(entity: entity, insertIntoManagedObjectContext: context) | |
} | |
public convenience init?(managedObjectContext: NSManagedObjectContext) { | |
guard let entity = _<$sanitizedManagedObjectClassName$>.entity(managedObjectContext) else { return nil } | |
self.init(entity: entity, insertIntoManagedObjectContext: managedObjectContext) | |
} | |
// MARK: - Properties | |
<$foreach Attribute noninheritedAttributes do$> | |
<$if Attribute.hasDefinedAttributeType$> | |
<$if Attribute.hasScalarAttributeType$> | |
<$if Attribute.isReadonly$> | |
public var <$Attribute.name$>: NSNumber? | |
{ | |
self.willAccessValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) | |
let <$Attribute.name$> = self.primitiveValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? NSNumber | |
self.didAccessValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) | |
return <$Attribute.name$> | |
} | |
<$else$> | |
@NSManaged public | |
var <$Attribute.name$>: NSNumber? | |
<$endif$> | |
<$else$> | |
<$if Attribute.isReadonly$> | |
public var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$> | |
{ | |
self.willAccessValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) | |
let <$Attribute.name$> = self.primitiveValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) as? <$Attribute.objectAttributeType$> | |
self.didAccessValueForKey(<$sanitizedManagedObjectClassName$>Attributes.<$Attribute.name$>.rawValue) | |
return <$Attribute.name$> | |
} | |
<$else$> | |
@NSManaged public | |
var <$Attribute.name$>: <$Attribute.objectAttributeType$><$if Attribute.isOptional$>?<$endif$> | |
<$endif$> | |
<$endif$> | |
<$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.sanitizedManagedObjectClassName$><$if Relationship.isOptional$>?<$endif$> | |
<$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? { | |
guard let psc = managedObjectContext.persistentStoreCoordinator else { return nil } | |
let model = psc.managedObjectModel | |
let substitutionVariables = [<$if FetchRequest.hasBindings$><$foreach Binding FetchRequest.bindings do2$> | |
"<$Binding.name$>": <$Binding.name$>, | |
<$endforeach do2$><$endif$> | |
] | |
guard let fetchRequest = model.fetchRequestFromTemplateWithName("<$FetchRequest.name$>", substitutionVariables: substitutionVariables) else { | |
assert(false, "Can't find fetch request named \"<$FetchRequest.name$>\".") | |
return nil | |
} | |
var result: AnyObject? = nil | |
do { | |
let results = try managedObjectContext.executeFetchRequest(fetchRequest) | |
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.first | |
default: | |
print("WARN fetch request <$FetchRequest.name$>: 0 or 1 objects expected, \(results.count) found (substitutionVariables: \(substitutionVariables), results: \(results))") | |
} | |
} catch { | |
print("Error executing fetch request: \(error)") | |
} | |
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]? { | |
guard let psc = managedObjectContext.persistentStoreCoordinator else { return nil } | |
let model = psc.managedObjectModel | |
let substitutionVariables : [String : AnyObject] = [<$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 results = Array<AnyObject>() | |
do { | |
results = try managedObjectContext.executeFetchRequest(fetchRequest!) | |
} catch { | |
print("Error executing fetch request: \(error)") | |
} | |
return results | |
} | |
<$endif$> | |
<$endforeach do$> | |
<$foreach FetchedProperty noninheritedFetchedProperties do$> | |
@NSManaged public | |
let <$FetchedProperty.name$>: [<$FetchedProperty.entity.sanitizedManagedObjectClassName$>] | |
<$endforeach do$> | |
<$foreach Attribute noninheritedAttributes do$> | |
<$if (Attribute.hasScalarAttributeType && Attribute.scalarAttributeType != "nil")$> | |
public dynamic var <$Attribute.name$>Value: <$Attribute.scalarAttributeType$> { | |
get { | |
if let value = self.<$Attribute.name$> { | |
return <$Attribute.scalarAttributeType$>(value) | |
} | |
return <$Attribute.scalarAttributeType$>(0) | |
} | |
set { | |
self.<$Attribute.name$> = newValue | |
} | |
} | |
class func keyPathsForValuesAffecting<$Attribute.name.initialCapitalString$>Value() -> NSSet { | |
return NSSet(object: "<$Attribute.name$>") | |
} | |
<$endif$> | |
<$endforeach do$> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
generates scalar attributes