Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Commit

Permalink
(#6786) Removing the #interface method.
Browse files Browse the repository at this point in the history
Since constants are already being defined for each
interface, the #interface method does little but
provide another way to access the same data.

Reviewed-By: Nick Lewis
  • Loading branch information
Pieter van de Bruggen committed Mar 21, 2011
1 parent f67e7fa commit b187e07
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
29 changes: 9 additions & 20 deletions lib/puppet/interface.rb
Expand Up @@ -6,8 +6,10 @@ class Puppet::Interface

include Puppet::Interface::ActionManager
extend Puppet::Interface::ActionManager

# This is just so we can search for actions. We only use its
# list of directories to search.
# Can't we utilize an external autoloader, or simply use the $LOAD_PATH? -pvb
def self.autoloader
@autoloader ||= Puppet::Util::Autoload.new(:application, "puppet/interface")
end
Expand All @@ -30,35 +32,22 @@ def self.interfaces
end
end
end
@interfaces.keys
Puppet::Interface.constants.map { |c| c.to_s.downcase }
end

# Return an interface by name, loading from disk if necessary.
def self.interface(name)
@interfaces ||= {}
unless @interfaces[unify_name(name)]
require "puppet/interface/#{unify_name(name)}"
end
@interfaces[unify_name(name)]
rescue Exception => detail
puts detail.backtrace if Puppet[:trace]
$stderr.puts "Unable to find interface '#{name.to_s}': #{detail}."
def self.const_missing(name)
require "puppet/interface/#{name.to_s.downcase}"
const_get(name) if const_defined?(name)
rescue LoadError
nil
end

def self.register_interface(name, instance)
@interfaces ||= {}
@interfaces[unify_name(name)] = instance
const_set(name2const(name), instance)
end

def self.unload_interface(name)
@interfaces ||= {}
@interfaces.delete(unify_name(name))
const = name2const(name)
const_get(const)
remove_const(const)
rescue
# nothing - if the constant-getting fails, just return
remove_const(name2const(name)) rescue nil
end

def self.unify_name(name)
Expand Down
22 changes: 9 additions & 13 deletions spec/unit/interface_spec.rb
Expand Up @@ -14,7 +14,9 @@
end

it "should register itself" do
Puppet::Interface.expects(:register_interface).with { |name, inst| name == :me and inst.is_a?(Puppet::Interface) }
Puppet::Interface.expects(:register_interface).with do |name, inst|
name == :me and inst.is_a?(Puppet::Interface)
end
Puppet::Interface.new(:me)
end

Expand Down Expand Up @@ -46,6 +48,7 @@
Puppet::Interface.new(:me).default_format.should == :pson
end

# Why?
it "should create a class-level autoloader" do
Puppet::Interface.autoloader.should be_instance_of(Puppet::Util::Autoload)
end
Expand All @@ -54,14 +57,6 @@
Puppet::Interface.new(:me, :verb => "foo").verb.should == "foo"
end

it "should be able to register and return interfaces" do
$stderr.stubs(:puts)
face = Puppet::Interface.new(:me)
Puppet::Interface.unload_interface(:me) # to remove from the initial registration
Puppet::Interface.register_interface(:me, face)
Puppet::Interface.interface(:me).should equal(face)
end

it "should create an associated constant when registering an interface" do
$stderr.stubs(:puts)
face = Puppet::Interface.new(:me)
Expand All @@ -70,29 +65,30 @@
Puppet::Interface::Me.should equal(face)
end

# Why is unloading interfaces important?
it "should be able to unload interfaces" do
$stderr.stubs(:puts)
face = Puppet::Interface.new(:me)
Puppet::Interface.unload_interface(:me)
Puppet::Interface.interface(:me).should be_nil
Puppet::Interface.const_defined?(:Me).should be_false
end

it "should remove the associated constant when an interface is unregistered" do
$stderr.stubs(:puts)
face = Puppet::Interface.new(:me)
Puppet::Interface.unload_interface(:me)
lambda { Puppet::Interface.const_get("Me") }.should raise_error(NameError)
Puppet::Interface.const_defined?("Me").should be_false
end

it "should try to require interfaces that are not known" do
Puppet::Interface.expects(:require).with "puppet/interface/foo"
Puppet::Interface.interface(:foo)
Puppet::Interface.const_get(:Foo)
end

it "should not fail when requiring an interface fails" do
$stderr.stubs(:puts)
Puppet::Interface.expects(:require).with("puppet/interface/foo").raises LoadError
lambda { Puppet::Interface.interface(:foo) }.should_not raise_error
lambda { Puppet::Interface::Foo }.should_not raise_error
end

it "should be able to load all actions in all search paths"
Expand Down

0 comments on commit b187e07

Please sign in to comment.