Commits

Michael Granger  committed 3055728

Add a name attribute to plugins for introspection.

  • Participants
  • Parent commits 6319eb6

Comments (0)

Files changed (2)

File lib/pluggability.rb

 			Pluggability.log.debug "  no name-based variants for anonymous subclass %p" % [ subclass ]
 		end
 
-		keys.compact.uniq.each do |key|
+		keys.compact!
+		keys.uniq!
+
+		# Register it under each of its name variants
+		keys.each do |key|
 			Pluggability.log.info "Registering %s derivative of %s as %p" %
 				[ subclass.name, plugin_class.name, key ]
 			plugin_class.derivatives[ key ] = subclass
 		end
 
+		# Add a name attribute to it
+		class << subclass
+			attr_reader :plugin_name
+		end
+		subclass.instance_variable_set( :@plugin_name, keys.last )
+
 		super
 	end
 

File spec/pluggability_spec.rb

 				TestingPlugin.plugin_type
 			}.to raise_error( Pluggability::PluginError, /couldn't find plugin base/i )
 		end
+
+		it "knows what the simplest version of its plugin name is" do
+			expect( TestingPlugin.plugin_name ).to eq( 'testing' )
+		end
 	end
 
 
 			expect( Plugin.create('black_sheep') ).to be_an_instance_of( BlackSheep )
 		end
 
+		it "knows what the simplest version of its plugin name is" do
+			expect( BlackSheep.plugin_name ).to eq( 'black_sheep' )
+		end
 	end
 
 
 			expect( Plugin.create('loadable') ).to be_an_instance_of( Test::LoadablePlugin )
 		end
 
+		it "still knows what the simplest version of its plugin name is" do
+			expect( Test::LoadablePlugin.plugin_name ).to eq( 'loadable' )
+		end
 	end
 
 
 			expect( Plugin.derivatives['subsub'] ).to eq( SubSubPlugin )
 		end
 
+		it "still knows what the simplest version of its plugin name is" do
+			expect( SubSubPlugin.plugin_name ).to eq( 'subsub' )
+		end
 	end
 
 end