From e81fee4d9d904b40afd8fccf4ec236c3517509f0 Mon Sep 17 00:00:00 2001 From: Simone Carletti Date: Sat, 30 Jun 2012 22:13:57 +0200 Subject: [PATCH] Ability to specify a custom :adapter passing a Symbol instead of an instance of Class. --- CHANGELOG.md | 4 ++++ lib/whois/server.rb | 9 ++++++++- spec/whois/server_spec.rb | 16 ++++++++++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea779abf6..509b2d6a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## master + +* NEW: Ability to specify a custom :adapter passing a Symbol instead of an instance of Class. + ## Release 2.6.3 diff --git a/lib/whois/server.rb b/lib/whois/server.rb index f9a4a255b..3d592b6df 100644 --- a/lib/whois/server.rb +++ b/lib/whois/server.rb @@ -164,7 +164,9 @@ def self.define(type, allocation, host, options = {}) # def self.factory(type, allocation, host, options = {}) options = options.dup - (options.delete(:adapter) || Adapters::Standard).new(type, allocation, host, options) + adapter = options.delete(:adapter) || Adapters::Standard + adapter = Adapters.const_get(camelize(adapter)) unless adapter.respond_to?(:new) + adapter.new(type, allocation, host, options) end @@ -225,6 +227,11 @@ def self.guess(string) private + def self.camelize(string) + string.to_s.split("_").collect(&:capitalize).join + end + + def self.matches_tld?(string) string =~ /^\.(xn--)?[a-z0-9]+$/ end diff --git a/spec/whois/server_spec.rb b/spec/whois/server_spec.rb index 3f5cc8467..450e430f7 100644 --- a/spec/whois/server_spec.rb +++ b/spec/whois/server_spec.rb @@ -71,8 +71,20 @@ s.should be_a(Whois::Server::Adapters::Standard) end - it "accepts an :adapter option and returns an instance of given adapter" do - s = Whois::Server.factory(:tld, ".test", "whois.test", :adapter => Whois::Server::Adapters::None) + it "accepts an :adapter option as Class and returns an instance of given adapter" do + a = Class.new do + attr_reader :args + def initialize(*args) + @args = args + end + end + s = Whois::Server.factory(:tld, ".test", "whois.test", :adapter => a) + s.should be_a(a) + s.args.should == [:tld, ".test", "whois.test", {}] + end + + it "accepts an :adapter option as Symbol, load Class and returns an instance of given adapter" do + s = Whois::Server.factory(:tld, ".test", "whois.test", :adapter => :none) s.should be_a(Whois::Server::Adapters::None) end