OSDN Git Service

CoreDataCoreのcoreを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 extension CoreDataConfiguration {
12     static let resourceHistory = CoreDataConfiguration("ResourceHistory")
13 }
14
15 class ResourceHistoryDataStore: CoreDataAccessor, CoreDataManager {
16     static let core = CoreDataCore(.resourceHistory)
17     
18     static let `default` = ResourceHistoryDataStore(type: .reader)
19     class func oneTimeEditor() -> ResourceHistoryDataStore {
20         return ResourceHistoryDataStore(type: .editor)
21     }
22     
23     required init(type: CoreDataManagerType) {
24         context = ResourceHistoryDataStore.context(for: type)
25     }
26     deinit {
27         save()
28     }
29     
30     let context: NSManagedObjectContext
31 }
32
33 extension ResourceHistoryDataStore {
34     func resources(in minites: [Int], older: Date) -> [Resource] {
35         let p = NSPredicate(format: "minute IN %@ AND date < %@", minites, older as NSDate)
36         guard let resources = try? objects(with: Resource.entity, predicate: p)
37             else { return [] }
38         return resources
39     }
40     func cerateResource() -> Resource? {
41         return insertNewObject(for: Resource.entity)
42     }
43 }