OSDN Git Service

CoreDataIntormationのイニシャライザにデフォルトバリューをもたせた
[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 CoreDataIntormation {
12     static let resourceHistory = CoreDataIntormation("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         managedObjectContext =
26             type == .reader ? core.parentManagedObjectContext
27                 : core.editorManagedObjectContext()
28     }
29     deinit {
30         saveActionCore()
31     }
32     
33     let core = CoreDataCore.resourceHistory
34     var managedObjectContext: NSManagedObjectContext
35 }
36
37 extension ResourceHistoryDataStore {
38     func resources(in minites: [Int], older: Date) -> [Resource] {
39         let p = NSPredicate(format: "minute IN %@ AND date < %@", minites, older as NSDate)
40         guard let resources = try? objects(with: Resource.entity, predicate: p)
41             else { return [] }
42         return resources
43     }
44     func cerateResource() -> Resource? {
45         return insertNewObject(for: Resource.entity)
46     }
47 }