<commit_before>//
//  Context.swift
//  AlecrimCoreData
//
//  Created by <NAME> on 2014-06-25.
//  Copyright (c) 2014 Alecrim. All rights reserved.
//

import Foundation
import CoreData

public class Context {
    
    private let stack: Stack!
    internal let managedObjectContext: NSManagedObjectContext!
    
    public init?(managedObjectModelName: String? = nil, stackType: StackType = StackType.SQLite) {
        if let stack = Stack(managedObjectModelName: managedObjectModelName, stackType: stackType) {
            self.stack = stack
        }
        else {
            return nil
        }
        
        self.managedObjectContext = self.stack.mainManagedObjectContext
    }
    
    private init(parentContext: Context) {
        self.stack = parentContext.stack
        self.managedObjectContext = parentContext.stack.createBackgroundManagedObjectContext()
    }
    
}

extension Context {
    
    public func save() -> Bool {
        let (success, _) = self.save()
        return success
    }

    public func save() -> (Bool, NSError?) {
        return self.stack.saveManagedObjectContext(self.managedObjectContext)
    }
    
}

extension Context {
    
    public func undo() {
        self.managedObjectContext.undo()
    }
    
    public func redo() {
        self.managedObjectContext.redo()
    }
    
    public func reset() {
        self.managedObjectContext.reset()
    }
    
    public func rollback() {
        self.managedObjectContext.rollback()
    }
    
}

extension Context {
    
    public func perform(closure: () -> Void) {
        self.managedObjectContext.performBlock(closure)
    }
    
    public func performAndWait(closure: () -> Void) {
        self.managedObjectContext.performBlockAndWait(closure)
    }
    
}

extension Context {

    public var hasChanges: Bool { return self.managedObjectContext.hasChanges }
    public var undoManager: NSUndoManager? { return self.managedObjectContext.undoManager }
    
    public func commitEditing() -> Bool {
        return self.managedObjectContext.commitEditing()
    }
    
}

// MARK: - public global functions

public func performInBackground<T: Context>(parentContext: T, closure: (T) -> Void) {
    let backgroundContext = T(parentContext: parentContext)
    
    backgroundContext.perform {
        closure(backgroundContext)
    }
}<commit_msg>Fixed minor compatibility issue.<commit_after>//
//  Context.swift
//  AlecrimCoreData
//
//  Created by <NAME> on 2014-06-25.
//  Copyright (c) 2014 Alecrim. All rights reserved.
//

import Foundation
import CoreData

public class Context {
    
    private let stack: Stack!
    internal let managedObjectContext: NSManagedObjectContext!
    
    public init?(managedObjectModelName: String? = nil, stackType: StackType = StackType.SQLite) {
        if let stack = Stack(managedObjectModelName: managedObjectModelName, stackType: stackType) {
            self.stack = stack
        }
        else {
            return nil
        }
        
        self.managedObjectContext = self.stack.mainManagedObjectContext
    }
    
    private init(parentContext: Context) {
        self.stack = parentContext.stack
        self.managedObjectContext = parentContext.stack.createBackgroundManagedObjectContext()
    }
    
}

extension Context {
    
    public func save() -> Bool {
        let (success, _) = self.save()
        return success
    }

    public func save() -> (Bool, NSError?) {
        return self.stack.saveManagedObjectContext(self.managedObjectContext)
    }
    
}

extension Context {
    
    public func undo() {
        self.managedObjectContext.undo()
    }
    
    public func redo() {
        self.managedObjectContext.redo()
    }
    
    public func reset() {
        self.managedObjectContext.reset()
    }
    
    public func rollback() {
        self.managedObjectContext.rollback()
    }
    
}

extension Context {
    
    public func perform(closure: () -> Void) {
        self.managedObjectContext.performBlock(closure)
    }
    
    public func performAndWait(closure: () -> Void) {
        self.managedObjectContext.performBlockAndWait(closure)
    }
    
}

extension Context {

    public var hasChanges: Bool { return self.managedObjectContext.hasChanges }
    public var undoManager: NSUndoManager? { return self.managedObjectContext.undoManager }
    
    #if os(OSX)

    public func commitEditing() -> Bool {
        return self.managedObjectContext.commitEditing()
    }
    
    #endif
    
}

// MARK: - public global functions

public func performInBackground<T: Context>(parentContext: T, closure: (T) -> Void) {
    let backgroundContext = T(parentContext: parentContext)
    
    backgroundContext.perform {
        closure(backgroundContext)
    }
}
{'fraction_non_alphanumeric': 0.09230445752733389, 'fraction_numerical': 0.005046257359125316, 'mean_word_length': 2.50811209439528, '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': '670587', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1305, 'n_words': 294}