diff options
Diffstat (limited to 'lib/openssl.rb')
-rw-r--r-- | lib/openssl.rb | 64 |
1 files changed, 21 insertions, 43 deletions
diff --git a/lib/openssl.rb b/lib/openssl.rb index 4203d2a..91494d3 100644 --- a/lib/openssl.rb +++ b/lib/openssl.rb @@ -119,55 +119,33 @@ end # defined? DH module X509 class Name - def Name::new(arg) - type = arg.class - while type - method = "new_from_#{type.name.downcase}".intern - return Name::send(method, arg) if Name::respond_to? method - type = type.superclass - end - raise TypeError, "Don't how to make new #{self} from #{arg.class}" - ###Name::send("new_from_#{arg.class.name.downcase}", arg) - end - # - # Name::new_from_hash(hash) is built-in method - # - def Name::new_from_string(str) # we're expecting string like "/A=B/C=D/E=F" - hash = Hash::new - key = val = nil # speed optim. - ary = str.split("/") - ary.shift # first item is "" - so skip it - ary.each {|item| - key, val = item.split("=") - hash[key] = val - } - Name::new_from_hash(hash) - ###ary.collect! {|item| item.split("=") } - ###Name::new_from_array(ary) + class << self + alias new_from_array new + + def new(arg) + case arg + when String + new_from_string(arg) + else + new_from_array(arg) + end + end + + def new_from_string(str) # expecting string like "/A=B/C=D/E=F" + ary = str.split("/") + ary.shift # first item is "" - so skip it + ary.collect!{|item| item.split("=", 2) } + new_from_array(ary) + end end - def Name::new_from_array(ary) # [["A","B"],["C","D"],["E","F"]] - hash = Hash::new - ary.each {|key, val| - hash[key] = val - } - Name::new_from_hash(hash) - end - # - # to_h is built-in method - # def to_s # "/A=B/C=D/E=F" - hash = self.to_h str = "" - hash.keys.each do |key| - str += "/" + key + "=" + hash[key] - end + hash = self.to_a.each{|key, val| + str << "/" << key << "=" << val + } str end - - def to_a # [["A","B"],["C","D"],["E","F"]] - self.to_h.to_a - end end # Name class ExtensionFactory |