<commit_before>//
//  Binder.swift
//  Pods
//
//  Created by <NAME>   on 21/06/15.
//
//

import Foundation

import Block_KVO
import CoreData

public class Binder : NSObject {
    let data : NSObject
    let keyPath: String
    var format: String?
    let handler: HandlerProtocol
    let view: UIView
    
    var converters: [ConverterProtocol] = []
    var defaultValue: AnyObject?
    
    init (obj: NSObject, prop: String, handler:HandlerProtocol, view: UIView) {
        self.data = obj
        self.keyPath = prop
        self.handler = handler
        self.view = view
        
        super.init()
        
        if prop == "@" {
            self.setValue(obj)
        } else {
            let value : AnyObject? = getValue(obj, prop: prop)
            self.setValue(value)
        }
        
        
    }
    
    func observe () {
        
        if self.keyPath == "@" {
            self.setValue(self.data)
            return
        }
        
        self.observeProperty("self.data." + self.keyPath, withBlock: { (s, old, new) -> Void in
            self.setValue(new)
        });
        
        if let observable = handler as? ObservableHandlerProtocol {
            observable.observe(self.view, fn: { (value) -> Void in
                self.data.setValue(value, forKeyPath: self.keyPath)
            })
        }
        
        
    }
    
    func unobserve () {
        if let observable = handler as? ObservableHandlerProtocol {
            observable.unobserve(self.view)
        }
    }
    
    private func getValue (data: NSObject, prop: String) -> AnyObject? {
        
        if let dict = data as? NSDictionary {
            if let obj: AnyObject? = dict.objectForKey(prop) {
                return obj
            }
            
        } else {
            
            
            //if PropertyFinder.hasProperty(prop, object: data) {
                return data.valueForKeyPath(prop)
            //}<commit_msg>added a conditional check for properties in the binder class<commit_after>//
//  Binder.swift
//  Pods
//
//  Created by <NAME>   on 21/06/15.
//
//

import Foundation

import Block_KVO
import CoreData

public class Binder : NSObject {
    let data : NSObject
    let keyPath: String
    var format: String?
    let handler: HandlerProtocol
    let view: UIView
    
    var converters: [ConverterProtocol] = []
    var defaultValue: AnyObject?
    
    init (obj: NSObject, prop: String, handler:HandlerProtocol, view: UIView) {
        self.data = obj
        self.keyPath = prop
        self.handler = handler
        self.view = view
        
        super.init()
        
        if prop == "@" {
            self.setValue(obj)
        } else {
            let value : AnyObject? = getValue(obj, prop: prop)
            self.setValue(value)
        }
        
        
    }
    
    func observe () {
        
        if self.keyPath == "@" {
            self.setValue(self.data)
            return
        }
        
        self.observeProperty("self.data." + self.keyPath, withBlock: { (s, old, new) -> Void in
            self.setValue(new)
        });
        
        if let observable = handler as? ObservableHandlerProtocol {
            observable.observe(self.view, fn: { (value) -> Void in
                self.data.setValue(value, forKeyPath: self.keyPath)
            })
        }
        
        
    }
    
    func unobserve () {
        if let observable = handler as? ObservableHandlerProtocol {
            observable.unobserve(self.view)
        }
    }
    
    private func getValue (data: NSObject, prop: String) -> AnyObject? {
        
        if let dict = data as? NSDictionary {
            if let obj: AnyObject? = dict.objectForKey(prop) {
                return obj
            }
            
        } else {
            
            if prop.rangeOfString(".") != nil {
                return data.valueForKey(prop)
            } else {
                if PropertyFinder.hasProperty(prop, object: data) {
                    return data.valueForKeyPath(prop)
                }
            }
            
{'fraction_non_alphanumeric': 0.09682926829268293, 'fraction_numerical': 0.002926829268292683, 'mean_word_length': 1.4151943462897527, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5290211', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1146, 'n_words': 281}