From 847ac203f9c0b5fce299e87a63b0de5d3ef416f6 Mon Sep 17 00:00:00 2001 From: Nick Lewis Date: Tue, 22 Mar 2011 16:44:01 -0700 Subject: maint: Implement an InterfaceCollection class to manage interfaces Having an instance variable on class Interface is insufficient for Interface::Indirector. This also changes the semantics of "Interface.interface" to handle registration and loading actions, and for "Interface.new" to only instantiate an Interface. Thus, consumers of the API should typically use "Interface.interface", unless they have reasons to not want an interface automatically registered. Paired-With: Pieter van de Bruggen --- lib/puppet/interface/interface_collection.rb | 50 ++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 lib/puppet/interface/interface_collection.rb (limited to 'lib/puppet/interface/interface_collection.rb') diff --git a/lib/puppet/interface/interface_collection.rb b/lib/puppet/interface/interface_collection.rb new file mode 100644 index 000000000..47ed702aa --- /dev/null +++ b/lib/puppet/interface/interface_collection.rb @@ -0,0 +1,50 @@ +require 'puppet/interface' + +module Puppet::Interface::InterfaceCollection + @interfaces = {} + + def self.interfaces + unless @loaded + @loaded = true + $LOAD_PATH.each do |dir| + next unless FileTest.directory?(dir) + Dir.chdir(dir) do + Dir.glob("puppet/interface/*.rb").collect { |f| f.sub(/\.rb/, '') }.each do |file| + iname = file.sub(/\.rb/, '') + begin + require iname + rescue Exception => detail + puts detail.backtrace if Puppet[:trace] + raise "Could not load #{iname} from #{dir}/#{file}: #{detail}" + end + end + end + end + end + return @interfaces.keys + end + + def self.[](name) + @interfaces[underscorize(name)] if interface?(name) + end + + def self.interface?(name) + name = underscorize(name) + require "puppet/interface/#{name}" unless @interfaces.has_key? name + return @interfaces.has_key? name + rescue LoadError + return false + end + + def self.register(interface) + @interfaces[underscorize(interface.name)] = interface + end + + def self.underscorize(name) + unless name.to_s =~ /^[-_a-z]+$/i then + raise ArgumentError, "#{name.inspect} (#{name.class}) is not a valid interface name" + end + + name.to_s.downcase.split(/[-_]/).join('_').to_sym + end +end -- cgit