<commit_before>//
//  ResourceLoader.swift
//  Pods
//
//  Created by Andreas on 20.08.16.
//
//

import UIKit

class ResourceLoader: NSObject {

    static func image(named: String, bundle: Bundle? = nil, searchOptions: SearchOptions = BoxCat.defaultSearchOptions) -> UIImage? {
        
        let bundle = bundle ?? Bundle.main
        
        // find bundle with image
        if let image = UIImage(named: named, in: bundle, compatibleWith: nil) {
            return image
        }
        
        // search in sub bundles
        for subBundle in bundle.bundles.filteredBy(names: searchOptions.bundlesToSearchOnly ?? []) {
            if let image = ResourceLoader.image(named: named, bundle: subBundle) {
                return image
            }
        }
        
        // search in frameworks
        for subBundle in bundle.frameworks.filteredBy(names: searchOptions.frameworksToSearchOnly ?? []) {
            if let image = ResourceLoader.image(named: named, bundle: subBundle) {
                return image
            }
        }
        
        return nil
    }
    
    static func nib(named: String, bundle: Bundle? = nil, searchOptions: SearchOptions = BoxCat.defaultSearchOptions) -> UINib? {
        
        let bundle = bundle ?? Bundle.main
        
        if bundle.path(forResource: named, ofType: "nib") != nil {
            return UINib(nibName: named, bundle: bundle)
        }
        
        // search in sub bundles
        for subBundle in bundle.bundles {
            if let nib = ResourceLoader.nib(named: named, bundle: subBundle) {
                return nib
            }
        }
        
        // search in frameworks
        for subBundle in bundle.frameworks {
            if let nib = ResourceLoader.nib(named: named, bundle: subBundle) {
                return nib
            }
        }
        
        return nil
    }
    
    static func font(named: String, size: CGFloat) -> UIFont? {
        // load dynamically http://www.mokacoding.com/blog/sharing-assets-with-cocoapods-resource-bundle-and-dynamically-loaded-fonts/
        
        return nil
    }
}<commit_msg>add generic method for the resource lookup<commit_after>//
//  ResourceLoader.swift
//  Pods
//
//  Created by Andreas on 20.08.16.
//
//

import UIKit

class ResourceLoader: NSObject {

    static func image(named: String, bundle: Bundle? = nil, searchOptions: SearchOptions = BoxCat.defaultSearchOptions) -> UIImage? {
        
        let bundle = bundle ?? Bundle.main
        
        // find bundle with image
        if let image = UIImage(named: named, in: bundle, compatibleWith: nil) {
            return image
        }
        
        return lookupResource(named: named, in: bundle, searchOptions: searchOptions) { (resourceName, subBundle) -> UIImage? in
            return ResourceLoader.image(named: resourceName, bundle: subBundle)
        }
    }
    
    static func nib(named: String, bundle: Bundle? = nil, searchOptions: SearchOptions = BoxCat.defaultSearchOptions) -> UINib? {
        
        let bundle = bundle ?? Bundle.main
        
        if bundle.path(forResource: named, ofType: "nib") != nil {
            return UINib(nibName: named, bundle: bundle)
        }
        
        return lookupResource(named: named, in: bundle, searchOptions: searchOptions, lookupClosure: { (resourceName, subBundle) -> UINib? in
            return ResourceLoader.nib(named: resourceName, bundle: subBundle)
        })
    }
    
    static func font(named: String, size: CGFloat) -> UIFont? {
        // load dynamically http://www.mokacoding.com/blog/sharing-assets-with-cocoapods-resource-bundle-and-dynamically-loaded-fonts/
        
        return nil
    }
    
    // MARK: private methods
    
    private static func lookupResource<ResourceType>(named: String, in bundle: Bundle, searchOptions: SearchOptions, lookupClosure: (_ resourceName: String, _ subBundle: Bundle) -> ResourceType?) -> ResourceType? {
        
        // TODO: also sort by order specified in the bundlesToSearchOnly
        for subBundle in bundle.bundles.filteredBy(names: searchOptions.bundlesToSearchOnly ?? []) {
            if let resource = lookupClosure(named, subBundle) {
                return resource
            }
        }
        
        // search in frameworks
        for subBundle in bundle.frameworks.filteredBy(names: searchOptions.frameworksToSearchOnly ?? []) {
            if let resource = lookupClosure(named, subBundle) {
                return resource
            }
        }
        
        return nil
    }
    
}
{'fraction_non_alphanumeric': 0.09396853146853147, 'fraction_numerical': 0.0026223776223776225, 'mean_word_length': 1.9817589576547232, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7538815', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1216, 'n_words': 331}