<commit_before>//
//  NSManagedObjectExtensions.swift
//  AlecrimCoreData
//
//  Created by <NAME> on 2014-06-24.
//  Copyright (c) 2014, 2015 Alecrim. All rights reserved.
//

import Foundation
import CoreData

extension NSManagedObject {

    public final func inContext(_ otherContext: NSManagedObjectContext) throws -> Self {
        if self.managedObjectContext === otherContext {
            return self
        }
        
        if self.objectID.isTemporaryID {
            try otherContext.obtainPermanentIDs(for: [self])
        }
        
        let otherManagedObject = try otherContext.existingObject(with: self.objectID)
        
        return unsafeBitCast(otherManagedObject, to: type(of: self))
    }
    
}

extension NSManagedObject {
    
    public final func delete() {
        self.managedObjectContext!.delete(self)
    }
    
    public final func refresh(mergeChanges: Bool = true) {
        self.managedObjectContext!.refresh(self, mergeChanges: mergeChanges)
    }

}

extension NSManagedObject {
    
    // TODO: move this code to other place
    public class func isIn(values: Set<NSManagedObject>) -> NSComparisonPredicate {
        let rightExpressionConstantValues = values.map { NSExpression(forConstantValue: $0.objectID) }
        let rightExpression = NSExpression(forAggregate: rightExpressionConstantValues)
        let leftExpression = NSExpression(forKeyPath: "objectID")
        
        return NSComparisonPredicate(
            leftExpression: leftExpression,<commit_msg>NSManagedObject extension: using `unsafeDowncast` instead of `unsafeBitCast`<commit_after>//
//  NSManagedObjectExtensions.swift
//  AlecrimCoreData
//
//  Created by <NAME> on 2014-06-24.
//  Copyright (c) 2014, 2015 Alecrim. All rights reserved.
//

import Foundation
import CoreData

extension NSManagedObject {

    public final func inContext(_ otherContext: NSManagedObjectContext) throws -> Self {
        if self.managedObjectContext === otherContext {
            return self
        }
        
        if self.objectID.isTemporaryID {
            try otherContext.obtainPermanentIDs(for: [self])
        }
        
        let otherManagedObject = try otherContext.existingObject(with: self.objectID)
        
        return unsafeDowncast(otherManagedObject, to: type(of: self))
    }
    
}

extension NSManagedObject {
    
    public final func delete() {
        self.managedObjectContext!.delete(self)
    }
    
    public final func refresh(mergeChanges: Bool = true) {
        self.managedObjectContext!.refresh(self, mergeChanges: mergeChanges)
    }

}

extension NSManagedObject {
    
    // TODO: move this code to other place
    public class func isIn(values: Set<NSManagedObject>) -> NSComparisonPredicate {
        let rightExpressionConstantValues = values.map { NSExpression(forConstantValue: $0.objectID) }
        let rightExpression = NSExpression(forAggregate: rightExpressionConstantValues)
        let leftExpression = NSExpression(forKeyPath: "objectID")
        
        return NSComparisonPredicate(
            leftExpression: leftExpression,
{'fraction_non_alphanumeric': 0.08161290322580646, 'fraction_numerical': 0.01096774193548387, 'mean_word_length': 2.8811013767209013, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '3957507', 'n_tokens_mistral': 850, 'n_tokens_neox': 813, 'n_words': 201}