2 // MasterShipMapper.swift
5 // Created by Hori,Masaki on 2017/02/22.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
12 class MasterShipMapper: JSONMapper {
13 typealias ObjectType = MasterShip
15 let apiResponse: APIResponse
16 let configuration = MappingConfiguration(entity: MasterShip.entity,
17 dataKeys: ["api_data", "api_mst_ship"],
18 editorStore: ServerDataStore.oneTimeEditor())
20 required init(_ apiResponse: APIResponse) {
21 self.apiResponse = apiResponse
24 private lazy var masterSTypes: [MasterSType] = {
25 return ServerDataStore.default.sortedMasterSTypesById()
28 func handleExtraValue(_ value: JSON, forKey key: String, to masterShip: MasterShip) -> Bool {
29 if key != "api_stype" { return false }
31 guard let sType = value.int
33 print("MasterShipMapper: value is not Int")
36 setStype(sType, to: masterShip)
40 private func setStype(_ stypeID: Int, to masterShip: MasterShip) {
41 if masterShip.stype.id == stypeID { return }
42 guard let stype = masterSTypes.binarySearch(comparator: { $0.id ==? stypeID })
43 else { return print("MasterShipMapper: Can not find MasterSType") }
44 guard let masterSType = configuration.editorStore.object(with: stype.objectID) as? MasterSType
45 else { return print("MasterShipMapper: Can not convert to current moc object masterSType") }
46 masterShip.stype = masterSType