OSDN Git Service

CoreDataConfigurationをstaticに持たないようにした
[kcd/KCD.git] / KCD / ResourceHistoryDataStore.swift
1 //
2 //  ResourceHistoryDataStore.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/06.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class ResourceHistoryDataStore: CoreDataManager {
12     
13     static let core = CoreDataCore(CoreDataConfiguration("ResourceHistory"))
14     
15     static let `default` = ResourceHistoryDataStore(type: .reader)
16     
17     class func oneTimeEditor() -> ResourceHistoryDataStore {
18         
19         return ResourceHistoryDataStore(type: .editor)
20     }
21     
22     required init(type: CoreDataManagerType) {
23         
24         context = ResourceHistoryDataStore.context(for: type)
25     }
26     
27     deinit {
28         
29         save()
30     }
31     
32     let context: NSManagedObjectContext
33 }
34
35 extension ResourceHistoryDataStore {
36     
37     func resources(in minites: [Int], older: Date) -> [Resource] {
38         
39         let p = NSPredicate.empty
40             .and(NSPredicate(#keyPath(Resource.minute), valuesIn: minites))
41             .and(NSPredicate(#keyPath(Resource.date), lessThan: older))
42         
43         guard let resources = try? objects(of: Resource.entity, predicate: p) else { return [] }
44         
45         return resources
46     }
47     
48     func createResource() -> Resource? {
49         
50         return insertNewObject(for: Resource.entity)
51     }
52 }