<commit_before>//
//  Folder.swift
//  DataModel
//
//  Created by <NAME> on 7/1/17.
//  Copyright © 2017 Ranchero Software, LLC. All rights reserved.
//

import Foundation
import Data

public final class Folder: DisplayNameProvider, Container, UnreadCountProvider {

	public let account: Account
	public var children = [AnyObject]()
	var name: String?
	static let untitledName = NSLocalizedString("Untitled ƒ", comment: "Folder name")

	// MARK: - Fetching Articles
	
	public func fetchArticles() -> Set<Article> {
		
		return account.fetchArticles(folder: self)
	}
	
	// MARK: - DisplayNameProvider

	public var nameForDisplay: String {
		get {
			return name ?? Folder.untitledName

		}
	}

	// MARK: - UnreadCountProvider

	public var unreadCount = 0 {
		didSet {
			if unreadCount != oldValue {
				postUnreadCountDidChangeNotification()
			}
		}
	}

	// MARK: - Init

	init(account: Account, name: String?) {
		
		self.account = account
		self.name = name

		NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
	}

	// MARK: - Disk Dictionary

	private struct Key {
		static let name = "name"
		static let unreadCount = "unreadCount"
		static let children = "children"
	}

	convenience init?(account: Account, dictionary: [String: Any]) {

		let name = dictionary[Key.name] as? String
		self.init(account: account, name: name)

		if let childrenArray = dictionary[Key.children] as? [[String: Any]] {
			self.children = Folder.objects(with: childrenArray, account: account)
		}

		if let savedUnreadCount = dictionary[Key.unreadCount] as? Int {
			self.unreadCount = savedUnreadCount
		}
	}

	var dictionary: [String: Any] {
		get {
			var d = [String: Any]()

			if let name = name {
				d[Key.name] = name
			}
			if unreadCount > 0 {
				d[Key.unreadCount] = unreadCount
			}

			let childObjects = children.flatMap { (child) -> [String: Any]? in

				if let feed = child as? Feed {
					return feed.dictionary
				}
				if let folder = child as? Folder, account.supportsSubFolders {
					return folder.dictionary
				}
				assertionFailure("Expected a feed or a folder.");
				return nil
			}

			if !childObjects.isEmpty {
				d[Key.children] = childObjects
			}

			return d
		}
	}
	
	// MARK: Feeds
	
	func addFeed(_ feed: Feed) -> Bool {
		
		// The feed has been uniqued at this point.
		// Return true in the case where the feed is already a child.
		
		if childrenContainsFeed(feed) {
			return true
		}
		children += [feed]
		return true
	}

	// MARK: Notifications

	@objc func unreadCountDidChange(_ note: Notification) {

		if let object = note.object {
			if objectIsChild(object as AnyObject) {
				updateUnreadCount()
			}
		}
	}

}

// MARK: - Private

private extension Folder {

	func updateUnreadCount() {
		
		unreadCount = calculateUnreadCount(children)
	}

	func childrenContainsFeed(_ feed: Feed) -> Bool {
		
		return children.contains(where: { (object) -> Bool in
			if let oneFeed = object as? Feed {
				if oneFeed.feedID == feed.feedID {
					return true
				}
			}
			return false
		})
	}
}
<commit_msg>Rename childrenContainsFeed to childrenContain. Add assertionFailure — expecting pointer equality.<commit_after>//
//  Folder.swift
//  DataModel
//
//  Created by <NAME> on 7/1/17.
//  Copyright © 2017 Ranchero Software, LLC. All rights reserved.
//

import Foundation
import Data

public final class Folder: DisplayNameProvider, Container, UnreadCountProvider {

	public let account: Account
	public var children = [AnyObject]()
	var name: String?
	static let untitledName = NSLocalizedString("Untitled ƒ", comment: "Folder name")

	// MARK: - Fetching Articles
	
	public func fetchArticles() -> Set<Article> {
		
		return account.fetchArticles(folder: self)
	}
	
	// MARK: - DisplayNameProvider

	public var nameForDisplay: String {
		get {
			return name ?? Folder.untitledName

		}
	}

	// MARK: - UnreadCountProvider

	public var unreadCount = 0 {
		didSet {
			if unreadCount != oldValue {
				postUnreadCountDidChangeNotification()
			}
		}
	}

	// MARK: - Init

	init(account: Account, name: String?) {
		
		self.account = account
		self.name = name

		NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
	}

	// MARK: - Disk Dictionary

	private struct Key {
		static let name = "name"
		static let unreadCount = "unreadCount"
		static let children = "children"
	}

	convenience init?(account: Account, dictionary: [String: Any]) {

		let name = dictionary[Key.name] as? String
		self.init(account: account, name: name)

		if let childrenArray = dictionary[Key.children] as? [[String: Any]] {
			self.children = Folder.objects(with: childrenArray, account: account)
		}

		if let savedUnreadCount = dictionary[Key.unreadCount] as? Int {
			self.unreadCount = savedUnreadCount
		}
	}

	var dictionary: [String: Any] {
		get {
			var d = [String: Any]()

			if let name = name {
				d[Key.name] = name
			}
			if unreadCount > 0 {
				d[Key.unreadCount] = unreadCount
			}

			let childObjects = children.flatMap { (child) -> [String: Any]? in

				if let feed = child as? Feed {
					return feed.dictionary
				}
				if let folder = child as? Folder, account.supportsSubFolders {
					return folder.dictionary
				}
				assertionFailure("Expected a feed or a folder.");
				return nil
			}

			if !childObjects.isEmpty {
				d[Key.children] = childObjects
			}

			return d
		}
	}
	
	// MARK: Feeds
	
	func addFeed(_ feed: Feed) -> Bool {
		
		// Return true in the case where the feed is already a child.
		
		if !childrenContain(feed) {
			children += [feed]
		}
		return true
	}

	// MARK: Notifications

	@objc func unreadCountDidChange(_ note: Notification) {

		if let object = note.object {
			if objectIsChild(object as AnyObject) {
				updateUnreadCount()
			}
		}
	}

}

// MARK: - Private

private extension Folder {

	func updateUnreadCount() {
		
		unreadCount = calculateUnreadCount(children)
	}

	func childrenContain(_ feed: Feed) -> Bool {
		
		return children.contains(where: { (object) -> Bool in
			if object === feed {
				return true
			}
			if let oneFeed = object as? Feed {
				if oneFeed.feedID == feed.feedID {
					assertionFailure("Expected feeds to match by pointer equality rather than by feedID.")
					return true
				}
			}
			return false
		})
	}
}

{'fraction_non_alphanumeric': 0.10801938408629044, 'fraction_numerical': 0.003126465530717524, 'mean_word_length': 3.0725652450668366, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3198127', 'n_tokens_mistral': 2419, 'n_tokens_neox': 1999, 'n_words': 593}