lib/httpx/options.rb in httpx-0.15.4 vs lib/httpx/options.rb in httpx-0.16.0

- old
+ new

@@ -28,10 +28,11 @@ :response_class => Class.new(Response), :headers_class => Class.new(Headers), :request_body_class => Class.new(Request::Body), :response_body_class => Class.new(Response::Body), :connection_class => Class.new(Connection), + :options_class => Class.new(self), :transport => nil, :transport_options => nil, :addresses => nil, :persistent => false, :resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym, @@ -39,116 +40,124 @@ }.freeze class << self def new(options = {}) # let enhanced options go through - return options if self == Options && options.class > self + return options if self == Options && options.class < self return options if options.is_a?(self) super end - def def_option(name, layout = nil, &interpreter) - attr_reader name + def method_added(meth) + super - if layout - class_eval(<<-OUT, __FILE__, __LINE__ + 1) - def #{name}=(value) - return if value.nil? + return unless meth =~ /^option_(.+)$/ - value = begin - #{layout} - end + optname = Regexp.last_match(1).to_sym - @#{name} = value - end + attr_reader(optname) + end + + def def_option(optname, *args, &block) + if args.size.zero? && !block_given? + class_eval(<<-OUT, __FILE__, __LINE__ + 1) + def option_#{optname}(v); v; end OUT + return + end - elsif interpreter - define_method(:"#{name}=") do |value| - return if value.nil? + deprecated_def_option(optname, *args, &block) + end - instance_variable_set(:"@#{name}", instance_exec(value, &interpreter)) + def deprecated_def_option(optname, layout = nil, &interpreter) + warn "DEPRECATION WARNING: using `def_option(#{optname})` for setting options is deprecated. " \ + "Define module OptionsMethods and `def option_#{optname}(val)` instead." + + if layout + class_eval(<<-OUT, __FILE__, __LINE__ + 1) + def option_#{optname}(value) + #{layout} + end + OUT + elsif block_given? + define_method(:"option_#{optname}") do |value| + instance_exec(value, &interpreter) end - else - attr_writer name end - - protected :"#{name}=" end end def initialize(options = {}) defaults = DEFAULT_OPTIONS.merge(options) defaults.each do |(k, v)| next if v.nil? begin - __send__(:"#{k}=", v) + value = __send__(:"option_#{k}", v) + instance_variable_set(:"@#{k}", value) rescue NoMethodError raise Error, "unknown option: #{k}" end end + freeze end - def_option(:origin, <<-OUT) + def option_origin(value) URI(value) - OUT + end - def_option(:headers, <<-OUT) - if self.headers - self.headers.merge(value) - else - Headers.new(value) - end - OUT + def option_headers(value) + Headers.new(value) + end - def_option(:timeout, <<-OUT) + def option_timeout(value) timeouts = Hash[value] if timeouts.key?(:loop_timeout) warn ":loop_timeout is deprecated, use :operation_timeout instead" timeouts[:operation_timeout] = timeouts.delete(:loop_timeout) end timeouts - OUT + end - def_option(:max_concurrent_requests, <<-OUT) - raise Error, ":max_concurrent_requests must be positive" unless value.positive? + def option_max_concurrent_requests(value) + raise TypeError, ":max_concurrent_requests must be positive" unless value.positive? value - OUT + end - def_option(:max_requests, <<-OUT) - raise Error, ":max_requests must be positive" unless value.positive? + def option_max_requests(value) + raise TypeError, ":max_requests must be positive" unless value.positive? value - OUT + end - def_option(:window_size, <<-OUT) + def option_window_size(value) Integer(value) - OUT + end - def_option(:body_threshold_size, <<-OUT) + def option_body_threshold_size(value) Integer(value) - OUT + end - def_option(:transport, <<-OUT) + def option_transport(value) transport = value.to_s - raise Error, "\#{transport} is an unsupported transport type" unless IO.registry.key?(transport) + raise TypeError, "\#{transport} is an unsupported transport type" unless IO.registry.key?(transport) transport - OUT + end - def_option(:addresses, <<-OUT) + def option_addresses(value) Array(value) - OUT + end %i[ params form json body ssl http2_settings - request_class response_class headers_class request_body_class response_body_class connection_class + request_class response_class headers_class request_body_class + response_body_class connection_class options_class io fallback_protocol debug debug_level transport_options resolver_class resolver_options persistent ].each do |method_name| def_option(method_name) end @@ -178,13 +187,12 @@ h1 = to_hash return self if h1 == h2 - merged = h1.merge(h2) do |k, v1, v2| - case k - when :headers, :ssl, :http2_settings, :timeout + merged = h1.merge(h2) do |_k, v1, v2| + if v1.respond_to?(:merge) && v2.respond_to?(:merge) v1.merge(v2) else v2 end end @@ -197,30 +205,27 @@ [ivar[1..-1].to_sym, instance_variable_get(ivar)] end Hash[hash_pairs] end - def initialize_dup(other) - self.headers = other.headers.dup - self.ssl = other.ssl.dup - self.request_class = other.request_class.dup - self.response_class = other.response_class.dup - self.headers_class = other.headers_class.dup - self.request_body_class = other.request_body_class.dup - self.response_body_class = other.response_body_class.dup - self.connection_class = other.connection_class.dup - end - - def freeze - super - - headers.freeze - ssl.freeze - request_class.freeze - response_class.freeze - headers_class.freeze - request_body_class.freeze - response_body_class.freeze - connection_class.freeze + if RUBY_VERSION > "2.4.0" + def initialize_dup(other) + instance_variables.each do |ivar| + instance_variable_set(ivar, other.instance_variable_get(ivar).dup) + end + end + else + def initialize_dup(other) + instance_variables.each do |ivar| + value = other.instance_variable_get(ivar) + value = case value + when Symbol, Fixnum, TrueClass, FalseClass # rubocop:disable Lint/UnifiedInteger + value + else + value.dup + end + instance_variable_set(ivar, value) + end + end end end end