betajs/betajs-scoped

View on GitHub
src/main/attach.js

Summary

Maintainability
C
1 day
Test Coverage
var Attach = (function () {  
/** 
 * This module provides functionality to attach the Scoped system to the environment.
 * 
 * @module Attach
 * @access private
 */
return { 
        
    __namespace: "Scoped",
    __revert: null,
    
    
    /**
     * Upgrades a pre-existing Scoped system to the newest version present. 
     * 
     * @param {string} namespace Optional namespace (default is 'Scoped')
     * @return {object} the attached Scoped system
     */
    upgrade: function (namespace/* : ?string */) {
        var current = Globals.get(namespace || Attach.__namespace);
        if (current && Helper.typeOf(current) === "object" && current.guid === this.guid && Helper.typeOf(current.version) === "string") {
            if (this.upgradable === false || current.upgradable === false)
                return current;
            var my_version = this.version.split(".");
            var current_version = current.version.split(".");
            var newer = false;
            for (var i = 0; i < Math.min(my_version.length, current_version.length); ++i) {
                newer = parseInt(my_version[i], 10) > parseInt(current_version[i], 10);
                if (my_version[i] !== current_version[i])
                    break;
            }
            return newer ? this.attach(namespace) : current;                
        } else
            return this.attach(namespace);        
    },


    /**
     * Attaches the Scoped system to the environment. 
     * 
     * @param {string} namespace Optional namespace (default is 'Scoped')
     * @return {object} the attached Scoped system
     */
    attach : function(namespace/* : ?string */) {
        if (namespace)
            Attach.__namespace = namespace;
        var current = Globals.get(Attach.__namespace);
        if (current === this)
            return this;
        Attach.__revert = current;
        if (current) {
            try {
                var exported = current.__exportScoped();
                this.__exportBackup = this.__exportScoped();
                this.__importScoped(exported);
            } catch (e) {
                // We cannot upgrade the old version.
            }
        }
        Globals.set(Attach.__namespace, this);
        return this;
    },
    

    /**
     * Detaches the Scoped system from the environment. 
     * 
     * @param {boolean} forceDetach Overwrite any attached scoped system by null.
     * @return {object} the detached Scoped system
     */
    detach: function (forceDetach/* : ?boolean */) {
        if (forceDetach)
            Globals.set(Attach.__namespace, null);
        if (typeof Attach.__revert != "undefined")
            Globals.set(Attach.__namespace, Attach.__revert);
        delete Attach.__revert;
        if (Attach.__exportBackup)
            this.__importScoped(Attach.__exportBackup);
        return this;
    },
    

    /**
     * Exports an object as a module if possible. 
     * 
     * @param {object} mod a module object (optional, default is 'module')
     * @param {object} object the object to be exported
     * @param {boolean} forceExport overwrite potentially pre-existing exports
     * @return {object} the Scoped system
     */
    exports: function (mod, object, forceExport) {
        mod = mod || (typeof module != "undefined" ? module : null);
        if (typeof mod == "object" && mod && "exports" in mod && (forceExport || mod.exports === this || !mod.exports || Helper.isEmpty(mod.exports)))
            mod.exports = object || this;
        return this;
    }    

};}).call(this);