OSDN Git Service

c7a584ffb306f4181738363d56b3011f58b0852b
[kcd/KCD.git] / KCD / BasicMapper.swift
1 //
2 //  BasicMapper.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/24.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class BasicMapper: JSONMapper {
12         
13     let apiResponse: APIResponse
14     let configuration: MappingConfiguration<Basic>
15     
16     required init(_ apiResponse: APIResponse) {
17         
18         self.apiResponse = apiResponse
19         self.configuration = MappingConfiguration(entity: Basic.entity,
20                                                   dataKeys: BasicMapper.dataKeys(apiResponse),
21                                                   editorStore: ServerDataStore.oneTimeEditor())
22     }
23     
24     private class func dataKeys(_ apiResponse: APIResponse) -> [String] {
25         
26         switch apiResponse.api.endpoint {
27             
28         case .port: return ["api_data", "api_basic"]
29             
30         case .basic: return ["api_data"]
31             
32         default:
33             
34             Logger.shared.log("Missing API: \(apiResponse.api)")
35             
36             return ["api_data"]
37             
38         }
39     }
40     
41     func commit() {
42         
43         configuration.editorStore.async(execute: commintInContext)
44     }
45     
46     private func commintInContext() {
47         
48         guard let store = configuration.editorStore as? ServerDataStore,
49             let basic = store.basic() ?? store.createBasic() else {
50                 
51                 Logger.shared.log("Can not Get Basic")
52                 
53                 return
54         }
55         
56         registerElement(data, to: basic)
57     }
58 }