Instantly share code, notes, and snippets.

Embed
What would you like to do?
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