lib/usher/interface.rb in usher-0.5.11 vs lib/usher/interface.rb in usher-0.5.12
- old
+ new
@@ -1,43 +1,32 @@
-# From Extlib
-module CamelCaseMixin
- def camel_case
- return self if self !~ /_/ && self =~ /[A-Z]+.*/
- split('_').map{|e| e.capitalize}.join
- end
-end
-
-# TODO: refactoring: I suggest to use usher/interfaces/rack.rb instead of
-# usher/interface/rack_interface.rb, it will enable me to simplify this code
class Usher
module Interface
- # Get root directory of interfaces of path to specified interface
- def self.interface_directory
- File.join(File.dirname(__FILE__), "interface")
- end
- # path to file
- def self.interface_path(name)
- File.join(self.interface_directory, "#{name}_interface.rb")
+ InterfaceRegistry = {}
+
+ def self.register(name, cls)
+ InterfaceRegistry[name] = cls
end
+ register(:email, File.join(File.dirname(__FILE__), 'interface', 'email'))
+ register(:merb, File.join(File.dirname(__FILE__), 'interface', 'merb'))
+ register(:rails20, File.join(File.dirname(__FILE__), 'interface', 'rails20'))
+ register(:rails22, File.join(File.dirname(__FILE__), 'interface', 'rails22'))
+ register(:rails23, File.join(File.dirname(__FILE__), 'interface', 'rails23'))
+ register(:rack, File.join(File.dirname(__FILE__), 'interface', 'rack'))
+ register(:rails3, File.join(File.dirname(__FILE__), 'interface', 'rails3'))
+ register(:text, File.join(File.dirname(__FILE__), 'interface', 'text'))
+
# Usher::Interface.for(:rack, &block)
def self.for(name, &block)
- if File.exist?(self.interface_path(name))
- require self.interface_path(name)
- snake_cased = "#{name}_interface".extend(CamelCaseMixin)
- Usher::Interface.const_get(snake_cased.camel_case).new(&block)
+ name = name.to_sym
+ if InterfaceRegistry[name]
+ require InterfaceRegistry[name]
+ const = Usher::Interface.const_get(File.basename(InterfaceRegistry[name]).to_s.split(/_/).map{|e| e.capitalize}.join)
+ const.new(&block)
else
- raise ArgumentError, "Interface #{name} doesn't exist. Choose one of: #{self.interfaces.inspect}"
+ raise ArgumentError, "Interface #{name.inspect} doesn't exist. Choose one of: #{InterfaceRegistry.keys.inspect}"
end
end
- # Array of symbols
- # Usher::Interface.interfaces
- # => [:email_interface, :merb_interface, :rack_interface, :rails2_2_interface, :rails2_3_interface, :rails3_interface, :text_interface]
- def self.interfaces
- Dir["#{self.interface_directory}/*.rb"].map do |interface|
- File.basename(interface).sub("_interface.rb", "").to_sym
- end
- end
end
end