<commit_before>/*
 * Backbone.Spark v1.1
 * Works with Backbone.js 0.9.2
 * Copyright (c) 2012 <NAME>
 * www.phdesign.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Backbone.Spark is largely based off Backbone.Mutators, see the included
 * NOTICE file for Backbone.Mutators license detils.
 */

/*
Usage
-----

var Demo = Backbone.Spark.Model.extend({

    defaults: function() {
      return {
        filePath: null
      };
    },

    sparks: {

        extn: function(key, value, options, set) {
            if (arguments.length === 0) {
                var i = this.filePath.lastIndexOf('.');
                return i >= 0 ? this.filePath.substr(i) : '';
            } else {
                var filePath = this.get('filePath'),
                    i = filePath.lastIndexOf('.');

                if (i >= 0)
                    this.set('filePath', filePath.substr(0, i) + value);
            }
        }.dependsOn('filePath'),

        folderPath: function() {
            if (!this.filePath) return '';
            var i = this.filePath.lastIndexOf('/');
            return i >= 0 ? this.filePath.substring(0, i) : ''; 
        }.dependsOn('filePath'),

    }

});
*/
 
Backbone.Spark = (function (_, Backbone, undef){
    var Spark = {},
        _super = Backbone.Model.prototype;

    // Record the dependent properties so we can raise change events when the dependents change.
    // This code is largely a copy of the Ember property() code.
    Function.prototype.dependsOn = function() {
        this._dependentKeys = Array.prototype.slice.call(arguments);
        this.noCache = function() {
            this._noCache = true;
            return this;
        };
        return this;
    };

    function triggerChanges(model, options) {
        var spark, sparkFunc, key, dependent, changes = [];

        for (spark in this.sparks) {
            sparkFunc = this.sparks[spark];
            if (sparkFunc._dependentKeys !== undef) {
                if (_.any(sparkFunc._dependentKeys, this.hasChanged, this))
                    changes.push(spark);
            }
        }

        _.each(changes, function(spark) {
            // Invalidate cache
            delete this._cache[spark];
            // Trigger change
            this.trigger('change:' + spark, this, this.get(spark));
        }, this);
    }

    // Extend the backbone model so that we can override the constructor and provide
    // event listeners for the dependent properties.
    Spark.Model = Backbone.Model.extend({

        constructor: function Base(attributes, options) {
            _super.constructor.apply(this, arguments);

            // For caching spark values
            this._cache = {};
            // Monitor for change events to the dependents
            this.on('change', triggerChanges, this);
        },

        get: function (attr) {
            var val,
                hasSparks = this.sparks !== undef;

            // If the attribute is a spark return the result of the function
            if (hasSparks === true && _.isFunction(this.sparks[attr]) === true) {
                // Check for cached value
                if (this._cache.hasOwnProperty(attr))
                    return this._cache[attr]; 
                val = _.bind(this.sparks[attr], this)();
                // Update cache
                if (this.sparks[attr]._noCache !== true)
                    this._cache[attr] = val;
                return val;
            }

            // Otherwise just delegate to the standard backbone get function.
            return _super.get.call(this, attr);
        },

        set: function (key, value, options) {
            var hasSparks = this.sparks !== undef,
                ret = null,
                attrs = null,<commit_msg>Fix bug where cache property is created<commit_after>/*
 * Backbone.Spark v1.1
 * Works with Backbone.js 0.9.2
 * Copyright (c) 2012 <NAME>
 * www.phdesign.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Backbone.Spark is largely based off Backbone.Mutators, see the included
 * NOTICE file for Backbone.Mutators license detils.
 */

/*
Usage
-----

var Demo = Backbone.Spark.Model.extend({

    defaults: function() {
      return {
        filePath: null
      };
    },

    sparks: {

        extn: function(key, value, options, set) {
            if (arguments.length === 0) {
                var i = this.filePath.lastIndexOf('.');
                return i >= 0 ? this.filePath.substr(i) : '';
            } else {
                var filePath = this.get('filePath'),
                    i = filePath.lastIndexOf('.');

                if (i >= 0)
                    this.set('filePath', filePath.substr(0, i) + value);
            }
        }.dependsOn('filePath'),

        folderPath: function() {
            if (!this.filePath) return '';
            var i = this.filePath.lastIndexOf('/');
            return i >= 0 ? this.filePath.substring(0, i) : ''; 
        }.dependsOn('filePath'),

    }

});
*/
 
Backbone.Spark = (function (_, Backbone, undef){
    var Spark = {},
        _super = Backbone.Model.prototype;

    // Record the dependent properties so we can raise change events when the dependents change.
    // This code is largely a copy of the Ember property() code.
    Function.prototype.dependsOn = function() {
        this._dependentKeys = Array.prototype.slice.call(arguments);
        this.noCache = function() {
            this._noCache = true;
            return this;
        };
        return this;
    };

    function triggerChanges(model, options) {
        var spark, sparkFunc, key, dependent, changes = [];

        for (spark in this.sparks) {
            sparkFunc = this.sparks[spark];
            if (sparkFunc._dependentKeys !== undef) {
                if (_.any(sparkFunc._dependentKeys, this.hasChanged, this))
                    changes.push(spark);
            }
        }

        _.each(changes, function(spark) {
            // Invalidate cache
            delete this._cache[spark];
            // Trigger change
            this.trigger('change:' + spark, this, this.get(spark));
        }, this);
    }

    // Extend the backbone model so that we can override the constructor and provide
    // event listeners for the dependent properties.
    Spark.Model = Backbone.Model.extend({

        constructor: function Base(attributes, options) {
            // For caching spark values
            this._cache = {};

            _super.constructor.apply(this, arguments);

            // Monitor for change events to the dependents
            this.on('change', triggerChanges, this);
        },

        get: function (attr) {
            var val,
                hasSparks = this.sparks !== undef;

            // If the attribute is a spark return the result of the function
            if (hasSparks === true && _.isFunction(this.sparks[attr]) === true) {
                // Check for cached value
                if (this._cache.hasOwnProperty(attr))
                    return this._cache[attr]; 
                val = _.bind(this.sparks[attr], this)();
                // Update cache
                if (this.sparks[attr]._noCache !== true)
                    this._cache[attr] = val;
                return val;
            }

            // Otherwise just delegate to the standard backbone get function.
            return _super.get.call(this, attr);
        },

        set: function (key, value, options) {
            var hasSparks = this.sparks !== undef,
                ret = null,
                attrs = null,
{'fraction_non_alphanumeric': 0.11135701805474665, 'fraction_numerical': 0.004426324985439721, 'mean_word_length': 1.8374091209517516, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '4587361', 'n_tokens_mistral': 2342, 'n_tokens_neox': 2218, 'n_words': 707}