Browse files

Ability to specify a custom :adapter passing a Symbol instead of an i…

…nstance of Class.
  • Loading branch information...
1 parent 934982e commit e81fee4d9d904b40afd8fccf4ec236c3517509f0 @weppos committed Jun 30, 2012
Showing with 26 additions and 3 deletions.
  1. +4 −0 CHANGELOG.md
  2. +8 −1 lib/whois/server.rb
  3. +14 −2 spec/whois/server_spec.rb
View
4 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
View
9 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
View
16 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

0 comments on commit e81fee4

Please sign in to comment.