]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#446] Updated to Popcorn.isArray(); Changes per last review;
authorrwldrn <waldron.rick@gmail.com>
Tue, 5 Apr 2011 18:31:04 +0000 (14:31 -0400)
committerrwldrn <waldron.rick@gmail.com>
Tue, 5 Apr 2011 18:34:55 +0000 (14:34 -0400)
popcorn.js
test/popcorn.unit.js

index af0c1bfdb73505ab7d9f4b54387f30dacfb96236..c9427b4f571302d0614798d37e77828c6c9fe7a0 100644 (file)
       for ( registryIdx = 0; registryIdx < registryLen; registryIdx++ ) {
         if ( Popcorn.registry[ registryIdx ].name === name ) {
           Popcorn.registry.splice( registryIdx, 1 );
-          delete Popcorn.registryHash[ name ];
+          delete Popcorn.registryByName[ name ];
 
           // delete the plugin
           delete obj[ name ];
   Popcorn.manifest = {};
   //  Plugins are registered
   Popcorn.registry = [];
-  Popcorn.registryHash = {};
+  Popcorn.registryByName = {};
   //  An interface for extending Popcorn
   //  with plugin functionality
   Popcorn.plugin = function( name, definition, manifest ) {
     var entry = {
       fn: plugin[ name ],
       definition: definition,
-      base_definition: definition,
+      base: definition,
       parents: [],
       name: name
     };
     Popcorn.registry.push( entry );
-    Popcorn.registryHash[ name ] = entry;
+    Popcorn.registryByName[ name ] = entry;
 
     return plugin;
   };
 
-  Popcorn.pluginInherit = function( name, parentNames, definition, manifest ) {
-    function getDefinition( p ) {
-      var hash = Popcorn.registryHash;
-      if ( hasOwn.call( hash, p ) ) {
-        return hash[ p ];
-      }
-      Popcorn.error( "Plugin "+ name +" can't inherit from "+ p +", which doesn't exist" );
+       //      Popcorn Plugin Inheritance Helper Methods
+       //      Internal use only
+  Popcorn.plugin.getDefinition = function( name ) {
+
+    var registry = Popcorn.registryByName;
+    
+    if ( registry[ name ] ) {
+      return registry[ name ];
     }
 
+    Popcorn.error( "Cannot inherit from "+ name +"; Object does not exist" );
+  }
+
+       //      Internal use only
+  Popcorn.plugin.delegate = function( instance, name, genus ) {
+
+    return function() {
+      genus.forEach( function( gene ) {
+        // The new plugin simply calls the delegated methods on
+        // all of its parents in the order they were specified.
+        gene[ name ] && gene[ name ].apply( instance, arguments );
+      });
+    };
+  };
+
+       //      Plugin inheritance
+  Popcorn.plugin.inherit = function( name, parents, definition, manifest ) {
+
+
     // Get the names of all of the ancestor classes, in the order that
     // we will be calling them. The override is for the class we're
     // currently defining, since it's not in the registry yet.
-    var ancestorNames = [];
+    var ancestors = [], 
+        pluginFn, entry;
+
     function getAncestors( name, override ) {
-      var parents = override || getDefinition( name ).parents;
+      var parents = override || Popcorn.plugin.getDefinition( name ).parents;
       for ( var i in parents ) {
         if ( hasOwn.call( parents, i ) ) {
           var p = parents[ i ];
           getAncestors( p );
-          if ( ancestorNames.indexOf( p ) === -1 ) {
-            ancestorNames.push( p );
+          if ( ancestors.indexOf( p ) === -1 ) {
+            ancestors.push( p );
           }
         }
       }
     }
-    getAncestors( name, Array.isArray( parentNames ) ? parentNames : [ parentNames ] );
-    ancestorNames.push( name );
+
+    getAncestors( name, Popcorn.isArray( parents ) ? parents : [ parents ] );
+    ancestors.push( name );
 
     // Now create the requested plugin under the reqested name.
-    var p = Popcorn.plugin( name, function( options ) {
-      var self = this;
-      function instantiate( p ) {
-        return (typeof p === "function") ? p.call( self, options ) : p;
-      }
-      function delegate( name ) {
-        return function() {
-          plugins.forEach( function( p ) {
-            // The new plugin simply calls the delegated methods on
-            // all of its parents in the order they were specified.
-            p[ name ] && p[ name ].apply( self, arguments );
-          } );
-        };
+    pluginFn = Popcorn.plugin( name, function( options ) {
+
+      var self = this, 
+          genus;
+
+      function instantiate( definition ) {
+        return definition.call && definition.call( self, options ) || definition;
       }
 
       // When the newly-defined plugin is instantiated, it must
       // explicitly instantiate all of its ancestors.
-      var plugins = ancestorNames.map( function( name ) {
-        return instantiate( getDefinition( name ).base_definition );
-      } );
+      genus = ancestors.map( function( name ) {
+        return instantiate( Popcorn.plugin.getDefinition( name ).base );
+      });
+
       return {
-        _setup: delegate( "_setup" ),
-        start: delegate( "start" ),
-        end: delegate( "end" )
+        _setup: Popcorn.plugin.delegate( self, "_setup", genus ),
+        start: Popcorn.plugin.delegate( self, "start", genus ),
+        end: Popcorn.plugin.delegate( self, "end", genus )
       };
+      
     }, manifest || definition.manifest );
 
-    var entry = getDefinition( name );
-    entry.base_definition = definition;
-    entry.parents = parentNames;
+    entry = Popcorn.plugin.getDefinition( name );
+    entry.base = definition;
+    entry.parents = parents;
 
-    return p;
+    return pluginFn;
   };
 
+       // Augment Popcorn; 
+  Popcorn.inherit = Popcorn.plugin.inherit;
+
   // stores parsers keyed on filetype
   Popcorn.parsers = {};
 
index 237fba235d1a5cdd90844449469afc573c417264..e60e8f27c14f06f2cec377b13634ea005b2737e4 100644 (file)
@@ -876,7 +876,7 @@ test("Plugin Factory", function () {
 
   });
 
-  Popcorn.pluginInherit("executor_inherits", "executor", function (options) {
+  Popcorn.inherit("executor_inherits", "executor", function (options) {
     return {
       start: function() { ok(true, "child class method executed"); plus(); }
     };
@@ -936,7 +936,7 @@ test("Plugin Factory", function () {
     }
   });
 
-  Popcorn.pluginInherit("complicator_inherits", "executor", {
+  Popcorn.inherit("complicator_inherits", "executor", {
     start: function() { ok(true, "child class method executed"); plus(); }
   });
 
@@ -963,16 +963,16 @@ test("Plugin Factory", function () {
   Popcorn.plugin("B", function(options) {
     return { start: function() { counts.b++; } };
   });
-  Popcorn.pluginInherit("C", "B", function(options) {
+  Popcorn.inherit("C", "B", function(options) {
     return { start: function() { counts.c++; } };
   });
-  Popcorn.pluginInherit("D", ["A", "B"], function(options) {
+  Popcorn.inherit("D", ["A", "B"], function(options) {
     return { start: function() { counts.d++; } };
   });
-  Popcorn.pluginInherit("E", ["B"], function(options) {
+  Popcorn.inherit("E", ["B"], function(options) {
     return { start: function() { counts.e++; } };
   });
-  Popcorn.pluginInherit("F", ["E", "C"], function(options) {
+  Popcorn.inherit("F", ["E", "C"], function(options) {
     return { start: function() { counts.f++; } };
   });