From 88e3f2859173a61c0e310c60b3e966d6d7409ae6 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Sun, 14 Apr 2013 18:22:30 +0900 Subject: [PATCH] Directly define String#try_convert and Hash#try_convert in ruby 1.8. --- lib/http/cookie.rb | 48 +++++----------------------------- lib/http/cookie/ruby_compat.rb | 26 ++++++++++++++++++ 2 files changed, 33 insertions(+), 41 deletions(-) diff --git a/lib/http/cookie.rb b/lib/http/cookie.rb index 906bf62..759e910 100644 --- a/lib/http/cookie.rb +++ b/lib/http/cookie.rb @@ -31,40 +31,6 @@ class HTTP::Cookie expires max_age created_at accessed_at ] - - if String.respond_to?(:try_convert) - def check_string_type(object) - String.try_convert(object) - end - private :check_string_type - else - def check_string_type(object) - if object.is_a?(String) || - (object.respond_to?(:to_str) && (object = object.to_str).is_a?(String)) - object - else - nil - end - end - private :check_string_type - end - - if Hash.respond_to?(:try_convert) - def check_hash_type(object) - Hash.try_convert(object) - end - private :check_hash_type - else - def check_hash_type(object) - if object.is_a?(Hash) || - (object.respond_to?(:to_h) && (object = object.to_str).is_a?(Hash)) - object - else - nil - end - end - private :check_hash_type - end # :startdoc: # The cookie name. It may not be nil or empty. @@ -170,14 +136,14 @@ class HTTP::Cookie @created_at = @accessed_at = Time.now case argc = args.size when 1 - if attr_hash = check_hash_type(args.last) + if attr_hash = Hash.try_convert(args.last) args.pop else self.name, self.value = args # value is set to nil return end when 2..3 - if attr_hash = check_hash_type(args.last) + if attr_hash = Hash.try_convert(args.last) args.pop self.name, value = args else @@ -378,7 +344,7 @@ class HTTP::Cookie # See #name. def name=(name) - name = check_string_type(name) or + name = String.try_convert(name) or raise TypeError, "#{name.class} is not a String" if name.empty? raise ArgumentError, "cookie name cannot be empty" @@ -399,7 +365,7 @@ class HTTP::Cookie self.expires = UNIX_EPOCH return @value = '' end - value = check_string_type(value) or + value = String.try_convert(value) or raise TypeError, "#{value.class} is not a String" if value.match(/[\x00-\x1F\x7F]/) raise ArgumentError, "invalid cookie value" @@ -427,7 +393,7 @@ class HTTP::Cookie when DomainName @domain_name = domain else - domain = check_string_type(domain) or + domain = String.try_convert(domain) or raise TypeError, "#{domain.class} is not a String" if domain.start_with?('.') for_domain = true @@ -472,7 +438,7 @@ class HTTP::Cookie # See #path. def path=(path) - path = check_string_type(path) or + path = String.try_convert(path) or raise TypeError, "#{path.class} is not a String" @path = path.start_with?('/') ? path : '/' end @@ -538,7 +504,7 @@ class HTTP::Cookie case sec when Integer, nil else - str = check_string_type(sec) or + str = String.try_convert(sec) or raise TypeError, "#{sec.class} is not an Integer or String" /\A-?\d+\z/.match(str) or raise ArgumentError, "invalid Max-Age: #{sec.inspect}" diff --git a/lib/http/cookie/ruby_compat.rb b/lib/http/cookie/ruby_compat.rb index 6cee6fd..924f51c 100644 --- a/lib/http/cookie/ruby_compat.rb +++ b/lib/http/cookie/ruby_compat.rb @@ -12,6 +12,32 @@ class Array end unless method_defined?(:select!) end +class Hash + class << self + def try_convert(object) + if object.is_a?(Hash) || + (object.respond_to?(:to_h) && (object = object.to_str).is_a?(Hash)) + object + else + nil + end + end unless method_defined?(:try_convert) + end +end + +class String + class << self + def try_convert(object) + if object.is_a?(String) || + (object.respond_to?(:to_str) && (object = object.to_str).is_a?(String)) + object + else + nil + end + end unless method_defined?(:try_convert) + end +end + # In Ruby < 1.9.3 URI() does not accept a URI object. if RUBY_VERSION < "1.9.3" require 'uri'