lib/openwfe/utils.rb in openwferu-0.9.13 vs lib/openwfe/utils.rb in openwferu-0.9.14
- old
+ new
@@ -64,40 +64,23 @@
puts text
exit 1
end
#
- # Attempts a deep cloning of the object
- #
- def OpenWFE.copy (object)
-
- return nil if object == nil
-
- if object.kind_of?(Array)
- result = []
- object.each do |i|
- result << copy(i)
- end
- return result
- end
-
- if object.kind_of?(Hash)
- result = {}
- object.each do |k, v|
- result[copy(k)] = copy(v)
- end
- return result
- end
-
- return object.dup
- end
-
- #
# see
# http://wiki.rubygarden.org/Ruby/page/show/Make_A_Deep_Copy_Of_An_Object
#
+ # It's not perfect (that's why fulldup() uses it only in certain cases).
+ #
+ # For example :
+ #
+ # TypeError: singleton can't be dumped
+ # ./lib/openwfe/utils.rb:74:in `dump'
+ # ./lib/openwfe/utils.rb:74:in `deep_clone'
+ #
def OpenWFE.deep_clone (object)
+
Marshal::load(Marshal.dump(object))
end
#
# an automatic dup implementation attempt
@@ -123,22 +106,28 @@
d = REXML::Document.new object.to_s
return d if object.kind_of?(REXML::Document)
return d.root
end
- o = object.class.new
+ o = nil
+ begin
+ o = object.class.new
+ rescue ArgumentError
+ return deep_clone(object)
+ end
+
#
# some kind of collection ?
if object.kind_of?(Array)
object.each do |i|
o << fulldup(i)
end
elsif object.kind_of?(Hash)
object.each do |k, v|
- o[copy(k)] = fulldup(v)
+ o[fulldup(k)] = fulldup(v)
end
end
#
# duplicate the attributes of the object
@@ -192,10 +181,12 @@
# Returns an URI if the string is one, else returns nil.
# No exception is thrown by this method.
#
def OpenWFE.parse_uri (string)
+ return nil if string.split("\n").size > 1
+
begin
return URI::parse(string)
rescue Exception => e
end
@@ -455,9 +446,25 @@
return
end
container = lookup_attribute(container, key[0..i-1])
container[key[i+1..-1]] = value
+ end
+
+ #
+ # Returns true if this host is currently online (has access to the web /
+ # internet).
+ #
+ def online?
+
+ require 'open-uri'
+
+ begin
+ open("http://www.openwfe.org")
+ true
+ rescue SocketError => se
+ false
+ end
end
protected
def pop_key (key)