Skip to content

Commit

Permalink
Ability to specify a custom :adapter passing a Symbol instead of an i…
Browse files Browse the repository at this point in the history
…nstance of Class.
  • Loading branch information
weppos committed Jun 30, 2012
1 parent 934982e commit e81fee4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions 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

Expand Down
9 changes: 8 additions & 1 deletion lib/whois/server.rb
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions spec/whois/server_spec.rb
Expand Up @@ -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

Expand Down

0 comments on commit e81fee4

Please sign in to comment.