OSDN Git Service

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