OSDN Git Service

staticプロパティをインスタンスプロパティに変更
[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.self,
20                                                   dataKeys: BasicMapper.dataKeys(apiResponse),
21                                                   editorStore: ServerDataStore.oneTimeEditor(),
22                                                   ignoreKeys: ["api_comment_id", "api_member_id", "api_nickname_id"])
23     }
24     
25     private class func dataKeys(_ apiResponse: APIResponse) -> [String] {
26         
27         switch apiResponse.api.endpoint {
28             
29         case .port: return ["api_data", "api_basic"]
30             
31         case .basic: return ["api_data"]
32             
33         default:
34             
35             Logger.shared.log("Missing API: \(apiResponse.api)")
36             
37             return ["api_data"]
38             
39         }
40     }
41     
42     func commit() {
43         
44         configuration.editorStore.async(execute: commintInContext)
45     }
46     
47     private func commintInContext() {
48         
49         guard let store = configuration.editorStore as? ServerDataStore,
50             let basic = store.basic() ?? store.createBasic() else {
51                 
52                 Logger.shared.log("Can not Get Basic")
53                 
54                 return
55         }
56         
57         registerElement(data, to: basic)
58     }
59 }