OSDN Git Service

ObjectTypeを明示せず推論させるようにした
[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 enum BasicAPI: String {
25         
26         case getMemberBasic = "/kcsapi/api_get_member/basic"
27         case port = "/kcsapi/api_port/port"
28     }
29     
30     private class func dataKeys(_ apiResponse: APIResponse) -> [String] {
31         
32         guard let basicApi = BasicAPI(rawValue: apiResponse.api) else { return ["api_data"] }
33         
34         switch basicApi {
35         case .port: return ["api_data", "api_basic"]
36             
37         case .getMemberBasic: return ["api_data"]
38         }
39     }
40     
41     func commit() {
42         
43         let store = ServerDataStore.oneTimeEditor()
44         
45         guard let basic = store.basic() ?? store.createBasic() else {
46             
47             print("Can not Get Basic")
48             return
49         }
50         
51         registerElement(data, to: basic)
52     }
53 }