// // MasterShipMapper.swift // KCD // // Created by Hori,Masaki on 2017/02/22. // Copyright © 2017年 Hori,Masaki. All rights reserved. // import Cocoa import SwiftyJSON final class MasterShipMapper: JSONMapper { let apiResponse: APIResponse let configuration = MappingConfiguration(entity: MasterShip.entity, dataKeys: ["api_data", "api_mst_ship"], editorStore: ServerDataStore.oneTimeEditor()) required init(_ apiResponse: APIResponse) { self.apiResponse = apiResponse } private lazy var masterSTypes: [MasterSType] = { return ServerDataStore.default.sortedMasterSTypesById() }() func handleExtraValue(_ value: JSON, forKey key: String, to masterShip: MasterShip) -> Bool { if key != "api_stype" { return false } guard let sType = value.int else { return Logger.shared.log("MasterShipMapper: value is not Int", value: false) } setStype(sType, to: masterShip) return true } private func setStype(_ stypeID: Int, to masterShip: MasterShip) { if masterShip.stype.id == stypeID { return } guard let stype = masterSTypes.binarySearch(comparator: { $0.id ==? stypeID }) else { return Logger.shared.log("MasterShipMapper: Can not find MasterSType") } guard let masterSType = configuration.editorStore.object(of: MasterSType.entity, with: stype.objectID) else { return Logger.shared.log("MasterShipMapper: Can not convert to current moc object masterSType") } masterShip.stype = masterSType } }