Last active
May 26, 2018 18:30
-
-
Save gokselkoksal/00ce6e6a4d0835b092866e8b1f06b1d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Place: Unboxable { | |
static let dateAddedFormatter: DateFormatter = { | |
let formatter = DateFormatter() | |
formatter.dateFormat = "YYYY-mm-dd" | |
return formatter | |
}() | |
init(unboxer: Unboxer) throws { | |
name = try unboxer.unbox(key: "placeName") | |
let lat: Decimal = try unboxer.unbox(key: "lat") | |
let lon: Decimal = try unboxer.unbox(key: "lon") | |
coordinate = Coordinate(latitude: lat, longitude: lon) | |
dateAdded = try unboxer.unbox(key: "dateAdded", formatter: Place.dateAddedFormatter) | |
info = unboxer.unbox(key: "info") | |
} | |
} | |
extension Place: WrapCustomizable { | |
func keyForWrapping(propertyNamed propertyName: String) -> String? { | |
if propertyName == "name" { | |
return "placeName" | |
} else { | |
return propertyName | |
} | |
} | |
func wrap(context: Any?, dateFormatter: DateFormatter?) -> Any? { | |
let dateFormatter = dateFormatter ?? Place.dateAddedFormatter | |
guard var payload = try? Wrapper(context: context, dateFormatter: dateFormatter).wrap(object: self) else { | |
return nil | |
} | |
payload["coordinate"] = nil | |
payload["lat"] = coordinate.latitude | |
payload["lon"] = coordinate.latitude | |
return payload | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment