Permalink
Browse files

Array.wrap is very useful

  • Loading branch information...
1 parent d5b6a0b commit 53ba15fa55da30bd0352989561199b3b7ca8c01d @weppos committed Mar 2, 2011
View
@@ -1,6 +1,7 @@
require 'date'
class DateTime
+
# Don't remove method if exists or it will conflict with ActiveRecord.
# See http://github.com/weppos/whois/issues#issue/24
unless method_defined?(:to_time)
@@ -12,4 +13,33 @@ def to_time
self.offset == 0 ? ::Time.utc(year, month, day, hour, min, sec) : self
end
end
-end
+
+end
+
+
+class Array
+
+ # Wraps its argument in an array unless it is already an array (or array-like).
+ #
+ # Specifically:
+ #
+ # * If the argument is +nil+ an empty list is returned.
+ # * Otherwise, if the argument responds to +to_ary+ it is invoked, and its result returned.
+ # * Otherwise, returns an array with the argument as its single element.
+ #
+ # Array.wrap(nil) # => []
+ # Array.wrap([1, 2, 3]) # => [1, 2, 3]
+ # Array.wrap(0) # => [0]
+ #
+ # Extracted from ActiveSupport.
+ def self.wrap(object)
+ if object.nil?
+ []
+ elsif object.respond_to?(:to_ary)
+ object.to_ary
+ else
+ [object]
+ end
+ end unless respond_to?(:wrap)
+
+end
@@ -87,11 +87,9 @@ class JobswhoisVerisignGrsCom < Base
property_supported :nameservers do
- node("Name Server") do |values|
- [*values].reject { |value| value =~ /no nameserver/i }.map do |name|
- Nameserver.new(name.downcase)
- end
- end || []
+ Array.wrap(node("Name Server")).reject { |value| value =~ /no nameserver/i }.map do |name|
+ Nameserver.new(name.downcase)
+ end
end
@@ -108,11 +108,9 @@ class WhoisAfiliasInfo < Base
property_supported :nameservers do
- node("Name Server") do |values|
- [*values].delete_if(&:empty?).map do |name|
- Nameserver.new(name.downcase)
- end
- end || []
+ Array.wrap(node("Name Server")).reject(&:empty?).map do |name|
+ Nameserver.new(name.downcase)
+ end
end
@@ -100,11 +100,9 @@ class WhoisBiz < Base
property_supported :nameservers do
- node("Name Server") do |values|
- [*values].map do |name|
- Nameserver.new(name.downcase)
- end
- end || []
+ Array.wrap(node("Name Server")).map do |name|
+ Nameserver.new(name.downcase)
+ end
end
@@ -36,7 +36,7 @@ class WhoisCat < Base
property_supported :status do
if content_for_scanner =~ /Status:\s+(.+?)\n/
- [*$1.split(", ")]
+ Array.wrap($1.split(", "))
end
end
@@ -93,11 +93,9 @@ class WhoisCnnicCn < Base
property_supported :nameservers do
- node("Name Server") do |values|
- [*values].map do |name|
- Nameserver.new(name.downcase)
- end
- end || []
+ Array.wrap(node("Name Server")).map do |name|
+ Nameserver.new(name.downcase)
+ end
end
@@ -88,11 +88,9 @@ class WhoisCrsnicNet < Base
property_supported :nameservers do
- node("Name Server") do |values|
- [*values].reject { |value| value =~ / / }.map do |name|
- Answer::Nameserver.new(name.downcase)
- end
- end || []
+ Array.wrap(node("Name Server")).reject { |value| value =~ / / }.map do |name|
+ Answer::Nameserver.new(name.downcase)
+ end
end
@@ -85,11 +85,9 @@ class WhoisNicCc < Base
property_supported :nameservers do
- node("Name Server") do |values|
- [*values].reject { |value| value =~ /no nameserver/i }.map do |name|
- Nameserver.new(name.downcase)
- end
- end || []
+ Array.wrap(node("Name Server")).reject { |value| value =~ /no nameserver/i }.map do |name|
+ Nameserver.new(name.downcase)
+ end
end
@@ -86,11 +86,9 @@ class WhoisNicTv < Base
property_supported :nameservers do
- node("Name Server") do |values|
- [*values].reject { |value| value =~ /no nameserver/i }.map do |name|
- Nameserver.new(name.downcase)
- end
- end || []
+ Array.wrap(node("Name Server")).reject { |value| value =~ /no nameserver/i }.map do |name|
+ Nameserver.new(name.downcase)
+ end
end

0 comments on commit 53ba15f

Please sign in to comment.