NetNewsWire/Frameworks/Database/Extensions/Author+Database.swift

74 lines
1.7 KiB
Swift
Raw Normal View History

2017-07-11 05:54:00 +02:00
//
// Author+Database.swift
// Database
//
// Created by Brent Simmons on 7/8/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import Foundation
import Data
import RSDatabase
import RSParser
2017-07-11 05:54:00 +02:00
// MARK: - DatabaseObject
2017-09-14 02:40:25 +02:00
extension Author {
init?(row: FMResultSet) {
2017-09-14 02:40:25 +02:00
let authorID = row.string(forColumn: DatabaseKey.authorID)
let name = row.string(forColumn: DatabaseKey.name)
let url = row.string(forColumn: DatabaseKey.url)
let avatarURL = row.string(forColumn: DatabaseKey.avatarURL)
let emailAddress = row.string(forColumn: DatabaseKey.emailAddress)
self.init(authorID: authorID, name: name, url: url, avatarURL: avatarURL, emailAddress: emailAddress)
}
init?(parsedAuthor: ParsedAuthor) {
self.init(authorID: nil, name: parsedAuthor.name, url: parsedAuthor.url, avatarURL: parsedAuthor.avatarURL, emailAddress: parsedAuthor.emailAddress)
}
2017-09-14 06:41:01 +02:00
2017-12-02 23:20:58 +01:00
public static func authorsWithParsedAuthors(_ parsedAuthors: Set<ParsedAuthor>?) -> Set<Author>? {
2017-09-14 06:41:01 +02:00
guard let parsedAuthors = parsedAuthors else {
return nil
}
let authors = Set(parsedAuthors.compactMap { Author(parsedAuthor: $0) })
2017-09-14 06:41:01 +02:00
return authors.isEmpty ? nil: authors
}
}
2017-09-14 02:40:25 +02:00
extension Author: DatabaseObject {
public var databaseID: String {
return authorID
2017-09-14 02:40:25 +02:00
}
public func databaseDictionary() -> NSDictionary? {
2017-09-14 06:41:01 +02:00
let d = NSMutableDictionary()
2017-09-14 02:40:25 +02:00
2017-09-14 06:41:01 +02:00
d[DatabaseKey.authorID] = authorID
2017-09-14 06:41:01 +02:00
if let name = name {
d[DatabaseKey.name] = name
}
if let url = url {
d[DatabaseKey.url] = url
}
if let avatarURL = avatarURL {
d[DatabaseKey.avatarURL] = avatarURL
}
if let emailAddress = emailAddress {
d[DatabaseKey.emailAddress] = emailAddress
}
2017-09-14 06:41:01 +02:00
return (d.copy() as! NSDictionary)
}
}