2017-07-02 02:22:19 +02:00
|
|
|
//
|
|
|
|
// Author.swift
|
|
|
|
// DataModel
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/1/17.
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2017-07-13 22:38:47 +02:00
|
|
|
import RSCore
|
2017-07-02 02:22:19 +02:00
|
|
|
|
|
|
|
public struct Author: Hashable {
|
|
|
|
|
2017-08-21 00:56:58 +02:00
|
|
|
public let authorID: String // calculated
|
2017-07-02 02:22:19 +02:00
|
|
|
public let name: String?
|
|
|
|
public let url: String?
|
|
|
|
public let avatarURL: String?
|
|
|
|
public let emailAddress: String?
|
2017-07-03 19:29:44 +02:00
|
|
|
public let hashValue: Int
|
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
public init?(authorID: String?, name: String?, url: String?, avatarURL: String?, emailAddress: String?) {
|
2017-07-02 02:22:19 +02:00
|
|
|
|
|
|
|
if name == nil && url == nil && emailAddress == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
self.name = name
|
|
|
|
self.url = url
|
|
|
|
self.avatarURL = avatarURL
|
|
|
|
self.emailAddress = emailAddress
|
|
|
|
|
2017-07-03 19:29:44 +02:00
|
|
|
var s = name ?? ""
|
2017-07-02 02:22:19 +02:00
|
|
|
s += url ?? ""
|
|
|
|
s += avatarURL ?? ""
|
2017-07-03 19:29:44 +02:00
|
|
|
s += emailAddress ?? ""
|
2017-07-02 02:22:19 +02:00
|
|
|
self.hashValue = s.hashValue
|
2017-07-17 04:36:38 +02:00
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
if let authorID = authorID {
|
|
|
|
self.authorID = authorID
|
2017-07-17 04:36:38 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-08-21 02:46:15 +02:00
|
|
|
self.authorID = databaseIDWithString(s)
|
2017-07-13 22:38:47 +02:00
|
|
|
}
|
2017-07-02 02:22:19 +02:00
|
|
|
}
|
2017-12-02 23:20:58 +01:00
|
|
|
|
|
|
|
public struct Key {
|
|
|
|
static let authorID = "authorID"
|
|
|
|
static let name = "name"
|
|
|
|
static let url = "url"
|
|
|
|
static let avatarURL = "avatarURL"
|
|
|
|
static let emailAddress = "emailAddress"
|
|
|
|
}
|
|
|
|
|
|
|
|
public init?(dictionary: [String: Any]) {
|
|
|
|
|
|
|
|
self.init(authorID: dictionary[Key.authorID] as? String, name: dictionary[Key.name] as? String, url: dictionary[Key.url] as? String, avatarURL: dictionary[Key.avatarURL] as? String, emailAddress: dictionary[Key.emailAddress] as? String)
|
|
|
|
}
|
|
|
|
|
|
|
|
public var dictionary: [String: Any] {
|
|
|
|
|
|
|
|
var d = [String: Any]()
|
|
|
|
|
|
|
|
d[Key.authorID] = authorID
|
|
|
|
|
|
|
|
if let name = name {
|
|
|
|
d[Key.name] = name
|
|
|
|
}
|
|
|
|
if let url = url {
|
|
|
|
d[Key.url] = url
|
|
|
|
}
|
|
|
|
if let avatarURL = avatarURL {
|
|
|
|
d[Key.avatarURL] = avatarURL
|
|
|
|
}
|
|
|
|
if let emailAddress = emailAddress {
|
|
|
|
d[Key.emailAddress] = emailAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2017-07-03 19:29:44 +02:00
|
|
|
public static func ==(lhs: Author, rhs: Author) -> Bool {
|
2017-07-02 02:22:19 +02:00
|
|
|
|
2017-08-21 06:23:17 +02:00
|
|
|
return lhs.hashValue == rhs.hashValue && lhs.authorID == rhs.authorID
|
2017-07-02 02:22:19 +02:00
|
|
|
}
|
2017-12-02 23:20:58 +01:00
|
|
|
|
|
|
|
static func authorsWithDiskArray(_ diskArray: [[String: Any]]) -> Set<Author>? {
|
|
|
|
|
2018-01-28 03:50:48 +01:00
|
|
|
let authors = diskArray.compactMap { Author(dictionary: $0) }
|
2017-12-02 23:20:58 +01:00
|
|
|
return authors.isEmpty ? nil : Set(authors)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Set where Element == Author {
|
|
|
|
|
|
|
|
func diskArray() -> [[String: Any]]? {
|
|
|
|
|
|
|
|
if self.isEmpty {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return self.map{ $0.dictionary }
|
|
|
|
}
|
2017-07-02 02:22:19 +02:00
|
|
|
}
|