<commit_before>/**
 * Base functionality for relationships.
 * @module relationships
 */

var InternalSiestaError = require('./error').InternalSiestaError,
    Store = require('./store'),
    Operation = require('./operation/operation').Operation,
    util = require('./util'),
    defineSubProperty = util.defineSubProperty,
    _ = util._,
    Query = require('./query'),
    log = require('./operation/log'),
    notifications = require('./notifications'),
    wrapArrayForAttributes = notifications.wrapArray,
    ArrayObserver = require('../vendor/observe-js/src/observe').ArrayObserver,
    coreChanges = require('./changes'),
    ChangeType = coreChanges.ChangeType;

/**
 * @class  [Fault description]
 * @param {RelationshipProxy} proxy
 * @constructor
 */
function Fault(proxy) {
    var self = this;
    this.proxy = proxy;
    Object.defineProperty(this, 'isFault', {
        get: function () {
            return self.proxy.isFault;
        },
        enumerable: true,
        configurable: true
    });
}

_.extend(Fault.prototype, {
    get: function () {
        this.proxy.get.apply(this.proxy, arguments);
    },
    set: function () {
        this.proxy.set.apply(this.proxy, arguments);
    }
});


/**
 * @class  [RelationshipProxy description]
 * @param {Object} opts
 * @constructor
 */
function RelationshipProxy(opts) {
    this._opts = opts;
    if (!this) return new RelationshipProxy(opts);
    var self = this;
    this.fault = new Fault(this);
    this.object = null;
    this._id = undefined;
    this.related = null;
    Object.defineProperty(this, 'isFault', {
        get: function () {
            if (self._id) {
                return !self.related;
            } else if (self._id === null) {
                return false;
            }
            return true;
        },
        set: function (v) {
            if (v) {
                self._id = undefined;
                self.related = null;
            } else {
                if (!self._id) {
                    self._id = null;
                }
            }
        },
        enumerable: true,
        configurable: true
    });
    defineSubProperty.call(this, 'reverseModel', this._opts);
    defineSubProperty.call(this, 'forwardModel', this._opts);
    defineSubProperty.call(this, 'forwardName', this._opts);
    defineSubProperty.call(this, 'reverseName', this._opts);
    defineSubProperty.call(this, 'isReverse', this._opts);
    Object.defineProperty(this, 'isForward', {
        get: function () {
            return !self.isReverse;
        },
        set: function (v) {
            self.isReverse = !v;
        },
        enumerable: true,
        configurable: true
    });
    if (this._opts.isReverse === undefined && this._opts.isForward !== undefined) {
        this.isReverse = !this._opts.isForward;
    }
    else if (this._opts.isReverse === undefined && this._opts.isForward === undefined) {
        throw new InternalSiestaError('Must specify either isReverse or isForward when configuring relationship proxy.');
    }
}

_.extend(RelationshipProxy.prototype, {
    _dump: function (asJson) {
        var dumped = {};
    },
    install: function (obj) {
        if (obj) {
            if (!this.object) {
                this.object = obj;
                var self = this;<commit_msg>some proxy refactoring<commit_after>/**
 * Base functionality for relationships.
 * @module relationships
 */

var InternalSiestaError = require('./error').InternalSiestaError,
    Store = require('./store'),
    Operation = require('./operation/operation').Operation,
    util = require('./util'),
    defineSubProperty = util.defineSubProperty,
    _ = util._,
    Query = require('./query'),
    log = require('./operation/log'),
    notifications = require('./notifications'),
    wrapArrayForAttributes = notifications.wrapArray,
    ArrayObserver = require('../vendor/observe-js/src/observe').ArrayObserver,
    coreChanges = require('./changes'),
    ChangeType = coreChanges.ChangeType;

/**
 * A fault occurs when we try to access a property that has not been loaded from disk
 * @param {RelationshipProxy} proxy
 * @constructor
 */
function Fault(proxy) {
    var self = this;
    this.proxy = proxy;
    Object.defineProperty(this, 'isFault', {
        get: function () {
            return self.proxy.isFault;
        },
        enumerable: true,
        configurable: true
    });
}

_.extend(Fault.prototype, {
    get: function () {
        this.proxy.get.apply(this.proxy, arguments);
    },
    set: function () {
        this.proxy.set.apply(this.proxy, arguments);
    }
});


/**
 * @class  [RelationshipProxy description]
 * @param {Object} opts
 * @constructor
 */
function RelationshipProxy(opts) {
    this._opts = opts;
    var self = this;

    _.extend(this, {
        fault: new Fault(this),
        object: null,
        _id: undefined,
        related: null
    });

    Object.defineProperties(this, {
        isFault: {
            get: function () {
                if (self._id) {
                    return !self.related;
                } else if (self._id === null) {
                    return false;
                }
                return true;
            },
            set: function (v) {
                if (v) {
                    self._id = undefined;
                    self.related = null;
                } else {
                    if (!self._id) {
                        self._id = null;
                    }
                }
            },
            enumerable: true,
            configurable: true
        },
        isForward: {
            get: function () {
                return !self.isReverse;
            },
            set: function (v) {
                self.isReverse = !v;
            },
            enumerable: true
        }
    });

   

    defineSubProperty.call(this, 'reverseModel', this._opts);
    defineSubProperty.call(this, 'forwardModel', this._opts);
    defineSubProperty.call(this, 'forwardName', this._opts);
    defineSubProperty.call(this, 'reverseName', this._opts);
    defineSubProperty.call(this, 'isReverse', this._opts);

    if (this._opts.isReverse === undefined && this._opts.isForward !== undefined) {
        this.isReverse = !this._opts.isForward;
    }
    else if (this._opts.isReverse === undefined && this._opts.isForward === undefined) {
        throw new InternalSiestaError('Must specify either isReverse or isForward when configuring relationship proxy.');
    }
}

_.extend(RelationshipProxy.prototype, {
    _dump: function (asJson) {
        var dumped = {};
    },
    install: function (obj) {
        if (obj) {
            if (!this.object) {
                this.object = obj;
                var self = this;
{'fraction_non_alphanumeric': 0.13069277555258862, 'fraction_numerical': 0.0, 'mean_word_length': 2.001780943900267, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1011597', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1844, 'n_words': 410}