<commit_before>//
//  ManagedRoom.swift
//  HomeControl
//
//  Created by <NAME> on 29/12/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import CoreData

public final class ManagedRoom: NSManagedObject {

    //MARK: Properties

    @NSManaged public private(set) var id: Int32
    @NSManaged public private(set) var name: String

    public var immutable: Room {
        let immutableLights = lights?.map {
            $0.immutable
        }

        let immutableBlinds = blinds?.map {
            $0.immutable
        }

        let immutableConsumers = consumers?.map {
            $0.immutable
        }

        return Room(id: UInt(id), name: name, lights: immutableLights, blinds: immutableBlinds, consumers: immutableConsumers)
    }

    //MARK: Relationships

    @NSManaged public private(set) var lights: Set<ManagedLight>?
    @NSManaged public private(set) var blinds: Set<ManagedBlind>?
    @NSManaged public private(set) var consumers: Set<ManagedConsumer>?

    //MARK: ManagedRoom

    public static func insert(room: Room, intoContext context: NSManagedObjectContext) -> ManagedRoom {
        let managedRoom: ManagedRoom = context.insertObject()
        managedRoom.id = Int32(room.id)
        managedRoom.name = room.name

        if let lights = room.lights {
            managedRoom.lights = Set(lights.map {
                ManagedLight.insert($0, intoContext: context)
            })
        }

        if let blinds = room.blinds {
            managedRoom.blinds = Set(blinds.map {
                ManagedBlind.insert($0, intoContext: context)
            })
        }

        if let consumers = room.consumers {
            managedRoom.consumers = Set(consumers.map {
                ManagedConsumer.insert($0, intoContext: context)
            })
        }

        return managedRoom
    }
}

//MARK: ManagedObjectType

extension ManagedRoom: ManagedObjectType {

    public static var entityName: String {
        return "Room"
    }

    public static var defaultSortDescriptors: [NSSortDescriptor] {
        return [NSSortDescriptor(key: "name", ascending: false)]
    }
}<commit_msg>Let ManagedRoom conform to StructConvertible<commit_after>//
//  ManagedRoom.swift
//  HomeControl
//
//  Created by <NAME> on 29/12/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import CoreData

public final class ManagedRoom: NSManagedObject {

    //MARK: Properties

    @NSManaged public private(set) var id: Int32
    @NSManaged public private(set) var name: String

    //MARK: Relationships

    @NSManaged public private(set) var lights: Set<ManagedLight>?
    @NSManaged public private(set) var blinds: Set<ManagedBlind>?
    @NSManaged public private(set) var consumers: Set<ManagedConsumer>?
}

//MARK: ManagedObjectType

extension ManagedRoom: ManagedObjectType {

    public static var entityName: String {
        return "Room"
    }

    public static var defaultSortDescriptors: [NSSortDescriptor] {
        return [NSSortDescriptor(key: "name", ascending: false)]
    }
}

//MARK: StructConvertible

extension ManagedRoom: StructConvertible {

    //MARK: Associated Types

    public typealias StructType = Room

    //MARK: Methods

    public func convertToStruct() -> StructType {
        let immutableLights = lights?.map {
            $0.immutable
        }

        let immutableBlinds = blinds?.map {
            $0.immutable
        }

        let immutableConsumers = consumers?.map {
            $0.immutable
        }

        return Room(id: UInt(id), name: name, lights: immutableLights, blinds: immutableBlinds, consumers: immutableConsumers)
    }

    public func configure(item: StructType) {
        guard let managedObjectContext = managedObjectContext else {
            fatalError("Managed object needs to be inserted into managed object context before it can be configured")
        }

        id = Int32(item.id)
        name = item.name

        if let lights = item.lights {
            self.lights = Set(lights.map {
                ManagedLight.insert($0, intoContext: managedObjectContext)
            })
        }

        if let blinds = item.blinds {
            self.blinds = Set(blinds.map {
                ManagedBlind.insert($0, intoContext: managedObjectContext)
            })
        }

        if let consumers = item.consumers {
            self.consumers = Set(consumers.map {
                ManagedConsumer.insert($0, intoContext: managedObjectContext)
            })
        }
    }
}
{'fraction_non_alphanumeric': 0.09289374025395411, 'fraction_numerical': 0.008910670527957229, 'mean_word_length': 2.3161004431314622, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '4805878', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1234, 'n_words': 331}