/
Employee.swift
69 lines (58 loc) · 2.02 KB
/
Employee.swift
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
//
// Created by Jesse Squires
// https://www.jessesquires.com
//
//
// Documentation
// https://jessesquires.github.io/JSQCoreDataKit
//
//
// GitHub
// https://github.com/jessesquires/JSQCoreDataKit
//
//
// License
// Copyright © 2015-present Jesse Squires
// Released under an MIT license: https://opensource.org/licenses/MIT
//
import CoreData
import Foundation
import JSQCoreDataKit
public final class Employee: NSManagedObject, CoreDataEntityProtocol {
// MARK: CoreDataEntityProtocol
public static let defaultSortDescriptors = [ NSSortDescriptor(key: "name", ascending: true) ]
// MARK: Properties
@NSManaged public var name: String
@NSManaged public var birthDate: Date
@NSManaged public var salary: NSDecimalNumber
@NSManaged public var company: Company?
// MARK: Init
public init(context: NSManagedObjectContext,
name: String,
birthDate: Date,
salary: NSDecimalNumber,
company: Company? = nil) {
super.init(entity: Self.entity(context: context), insertInto: context)
self.name = name
self.birthDate = birthDate
self.salary = salary
self.company = company
}
@objc
override private init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
super.init(entity: entity, insertInto: context)
}
public static func newEmployee(_ context: NSManagedObjectContext, company: Company? = nil) -> Employee {
let name = "Employee " + String(UUID().uuidString.split { $0 == "-" }.first!)
return Self(context: context,
name: name,
birthDate: Date.distantPast,
salary: NSDecimalNumber(value: Int.random(in: 0...100_000)),
company: company)
}
public static func fetchRequest(for company: Company) -> NSFetchRequest<Employee> {
let fetch = self.fetchRequest
fetch.predicate = NSPredicate(format: "company == %@", company)
return fetch
}
}