<commit_before><?php

namespace ActiveRedis;

abstract class Association 
{
	public static $poly = false;
	
	public $left;
	
	public $leftClass;
	public $rightClass;
	
	public $name;
	public $foreignKey;
	
	public $eager; // whether to autolaod on default
	
	function __construct($leftClass, $rightClass, $options = null) 
	{	
		$this->leftClass = $leftClass;
		$this->rightClass = $rightClass;
		
		if (is_array($options)) {
			foreach ($options as $var => $val) {
				$this->$var = $val;
			}
		}
		
		// Default name
		if (!$this->name) {
			$pieces = explode('\\', $this->rightClass);
			$this->name = lcfirst(array_pop($pieces));
			if ($this::$poly) {
				$this->name = Inflector::pluralize($this->name);
			}
		}
		
		// Default foreign key
		if (!$this->foreignKey) { 
			if ($this::$poly) {
				$this->foreignKey = Inflector::singularize($this->name) . '_ids';
			} else {
				$this->foreignKey = $this->name . '_id';
			}
		}
	}
	
	/**
	 * Attach this association to a table
	 */
	function attach(Table $table) {}
	
	/**
	 * Associate a left-side model with a right-side model
	 */
	function associate(Model $left, Model $right) {}
	
	/**
	 * Get the "right-side" objects associated with the "left-side"
	 */
	function associated(Model $left) {}
	
	/**
	 * Called whenever a "left-side" model is constructed
	 */
	function autoload(Model $left) 
	{
		return $this->eager ? $this->associated($left) : null;
	}
	
	/**
	 * Returned by the left model when a user accesses the association
	 */
	function delegate($left = null) {
		return $this->associated($left);
	}
}

class HasOne extends Association
{
	function associate(Model $left, Model $right) {
		$left->setAttribute($this->foreignKey, $right->primaryKeyValue());
	}
	
	function attach(Table $table) {
		$table->bind('beforeDelete', array($this, 'beforeDelete'));
	}
	
	function beforeDelete($left) {
		$left->setAttribute($this->foreignKey, null);
	}
	
	function associated(Model $left) {
		// get a model
		$rightClass = $this->rightClass;
		if ($id = $left->table()->get($this->foreignKey)) {
			return $rightClass::find($id);
		}<commit_msg>Restored Association::delegate() function to its former glory.<commit_after><?php

namespace ActiveRedis;

abstract class Association 
{
	public static $poly = false;
	
	public $left;
	
	public $leftClass;
	public $rightClass;
	
	public $name;
	public $foreignKey;
	
	public $eager; // whether to autolaod on default
	
	function __construct($leftClass, $rightClass, $options = null) 
	{	
		$this->leftClass = $leftClass;
		$this->rightClass = $rightClass;
		
		if (is_array($options)) {
			foreach ($options as $var => $val) {
				$this->$var = $val;
			}
		}
		
		// Default name
		if (!$this->name) {
			$pieces = explode('\\', $this->rightClass);
			$this->name = lcfirst(array_pop($pieces));
			if ($this::$poly) {
				$this->name = Inflector::pluralize($this->name);
			}
		}
		
		// Default foreign key
		if (!$this->foreignKey) { 
			if ($this::$poly) {
				$this->foreignKey = Inflector::singularize($this->name) . '_ids';
			} else {
				$this->foreignKey = $this->name . '_id';
			}
		}
	}
	
	/**
	 * Attach this association to a table
	 */
	function attach(Table $table) {}
	
	/**
	 * Associate a left-side model with a right-side model
	 */
	function associate(Model $left, Model $right) {}
	
	/**
	 * Get the "right-side" objects associated with the "left-side"
	 */
	function associated(Model $left) {}
	
	/**
	 * Called whenever a "left-side" model is constructed
	 */
	function autoload(Model $left) 
	{
		return $this->eager ? $this->associated($left) : null;
	}
	
	/**
	 * Returned by the left model when a user accesses the association
	 */
	function delegate($left = null) 
	{
		$this->left = $left;
		return $this;
	}
}

class HasOne extends Association
{
	function associate(Model $left, Model $right) {
		$left->setAttribute($this->foreignKey, $right->primaryKeyValue());
	}
	
	function attach(Table $table) {
		$table->bind('beforeDelete', array($this, 'beforeDelete'));
	}
	
	function beforeDelete($left) {
		$left->setAttribute($this->foreignKey, null);
	}
	
	function associated(Model $left) {
		// get a model
		$rightClass = $this->rightClass;
		if ($id = $left->table()->get($this->foreignKey)) {
			return $rightClass::find($id);
		}
{'fraction_non_alphanumeric': 0.16615960683360637, 'fraction_numerical': 0.0, 'mean_word_length': 3.475392670157068, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, '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': '4471244', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1389, 'n_words': 346}