2 // MasterShipMapper.swift
5 // Created by Hori,Masaki on 2017/02/22.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
12 final class MasterShipMapper: JSONMapper {
14 let apiResponse: APIResponse
15 let configuration = MappingConfiguration(entity: MasterShip.entity,
16 dataKeys: ["api_data", "api_mst_ship"],
17 editorStore: ServerDataStore.oneTimeEditor())
19 required init(_ apiResponse: APIResponse) {
21 self.apiResponse = apiResponse
24 private lazy var masterSTypes: [MasterSType] = {
26 return ServerDataStore.default.sortedMasterSTypesById()
29 func handleExtraValue(_ value: JSON, forKey key: String, to masterShip: MasterShip) -> Bool {
31 if key != "api_stype" { return false }
33 guard let sType = value.int else {
35 return Logger.shared.log("MasterShipMapper: value is not Int", value: false)
38 setStype(sType, to: masterShip)
43 private func setStype(_ stypeID: Int, to masterShip: MasterShip) {
45 if masterShip.stype.id == stypeID { return }
47 guard let stype = masterSTypes.binarySearch(comparator: { $0.id ==? stypeID }) else {
49 return Logger.shared.log("MasterShipMapper: Can not find MasterSType")
52 guard let masterSType = configuration.editorStore.object(of: MasterSType.entity, with: stype.objectID) else {
54 return Logger.shared.log("MasterShipMapper: Can not convert to current moc object masterSType")
57 masterShip.stype = masterSType