Skip to content

Instantly share code, notes, and snippets.

@gracietti
Created Jun 6, 2017
Embed
What would you like to do?
Example of an Entity with computed properties
import Foundation
import RealmSwift
import ObjectMapper
import ObjectMapper_Realm
class Event: Object, Mappable, Identifiable, RandomizePlaceholder, TrackableEntity {
dynamic var id: Int = 0
fileprivate dynamic var stringType: String = ""
fileprivate dynamic var title = ""
fileprivate dynamic var name = ""
dynamic var desc = ""
dynamic var url = ""
dynamic var eventImage = ""
dynamic var contactEmail = ""
dynamic var contactPhone = ""
dynamic var twitterId = ""
dynamic var photos = ""
dynamic var edition = 0
dynamic var country = ""
dynamic var region = ""
dynamic var city = ""
dynamic var placesId = ""
dynamic var venue = ""
dynamic var address1 = ""
dynamic var address2 = ""
dynamic var zipCode = ""
internal dynamic var placeHolderIndex = -1
dynamic var interestedCount: Int = 0
// MARK: Relationships
var dates = List<EventDate>()
var organizedBy: Member?
override static func ignoredProperties() -> [String] {
return ["eventImageURL", "detailedAddress"]
}
required convenience init?(map: Map) {
self.init()
}
override static func primaryKey() -> String? {
return "id"
}
func mapping(map: Map) {
if map.mappingType == .toJSON {
var id = self.id
id <- map["id"]
} else {
id <- map["id"]
}
name <- map["name"]
title <- map["title"]
desc <- map["description"]
url <- map["url"]
stringType <- map["type"]
eventImage <- map["hero_image.image"]
contactEmail <- map["contact_email"]
contactPhone <- map["contact_phone"]
twitterId <- map["twitter_id"]
photos <- map["photo_feed"]
edition <- map["edition"]
country <- map["country"]
region <- map["region"]
city <- map["city"]
placesId <- map["places_id"]
venue <- map["venue"]
address1 <- map["address_1"]
address2 <- map["address_2"]
zipCode <- map["zip_code"]
interestedCount <- map["members_interested_count"]
dates <- (map["dates"], ListTransform<EventDate>())
}
}
// MARK: Computed vars extension
extension Event {
var displayName: String {
if self.type == .summit {
return "SingularityU \(self.name) Summit"
}
return self.title
}
var principalLocation: String? {
return [city, region, country].emptyRemoved.joined(separator: ", ")
}
var detailedAddress: String {
var address = String()
if !self.address1.isEmpty {
address.append(self.address1)
}
if !self.address2.isEmpty {
address.append(", \(self.address2)")
}
if let location = self.principalLocation, !location.isEmpty {
address.append(" - \(location)")
}
if !self.zipCode.isEmpty {
address.append(" - \(self.zipCode)")
}
return address
}
var eventDate: String? {
guard let first = self.dates.first,
let last = self.dates.last,
let firstDate = first.day?.toDate(withFormat: "yyyy-MM-dd"),
let lastDate = last.day?.toDate(withFormat: "yyyy-MM-dd")
else { return nil }
if firstDate == lastDate {
return firstDate.toString(withFormat: "MMM dd, yyyy")
} else {
if firstDate.components.year != lastDate.components.year {
return firstDate.toString(withFormat: "MMM dd, yyyy - ") + lastDate.toString(withFormat: "MMM dd, yyyy")
} else if firstDate.components.month != lastDate.components.month {
return firstDate.toString(withFormat: "MMM dd - ") + lastDate.toString(withFormat: "MMM dd, yyyy")
} else {
return firstDate.toString(withFormat: "MMM dd - ") + lastDate.toString(withFormat: "dd, yyyy")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment