OSDN Git Service

タイポを修正
[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 extension CoreDataConfiguration {
12     
13     static let resourceHistory = CoreDataConfiguration("ResourceHistory")
14 }
15
16 final class ResourceHistoryDataStore: CoreDataAccessor, CoreDataManager {
17     
18     static let core = CoreDataCore(.resourceHistory)
19     
20     static let `default` = ResourceHistoryDataStore(type: .reader)
21     
22     class func oneTimeEditor() -> ResourceHistoryDataStore {
23         
24         return ResourceHistoryDataStore(type: .editor)
25     }
26     
27     required init(type: CoreDataManagerType) {
28         
29         context = ResourceHistoryDataStore.context(for: type)
30     }
31     
32     deinit {
33         
34         save()
35     }
36     
37     let context: NSManagedObjectContext
38 }
39
40 extension ResourceHistoryDataStore {
41     
42     func resources(in minites: [Int], older: Date) -> [Resource] {
43         
44         let p = NSPredicate.empty
45             .and(NSPredicate(#keyPath(Resource.minute), valuesIn: minites))
46             .and(NSPredicate(#keyPath(Resource.date), lessThan: older))
47         
48         guard let resources = try? objects(of: Resource.entity, predicate: p) else { return [] }
49         
50         return resources
51     }
52     
53     func createResource() -> Resource? {
54         
55         return insertNewObject(for: Resource.entity)
56     }
57 }