lib/httpx/options.rb in httpx-0.7.0 vs lib/httpx/options.rb in httpx-0.8.0
- old
+ new
@@ -23,27 +23,32 @@
@defined_options ||= []
end
def def_option(name, &interpreter)
defined_options << name.to_sym
- interpreter ||= ->(v) { v }
attr_reader name
- define_method(:"#{name}=") do |value|
- return if value.nil?
+ if interpreter
+ define_method(:"#{name}=") do |value|
+ return if value.nil?
- instance_variable_set(:"@#{name}", instance_exec(value, &interpreter))
- end
+ instance_variable_set(:"@#{name}", instance_exec(value, &interpreter))
+ end
- protected :"#{name}="
+ define_method(:"with_#{name}") do |value|
+ merge(name => instance_exec(value, &interpreter))
+ end
+ else
+ attr_writer name
- define_method(:"with_#{name}") do |value|
- other = dup
- other.send(:"#{name}=", other.instance_exec(value, &interpreter))
- other
+ define_method(:"with_#{name}") do |value|
+ merge(name => value)
+ end
end
+
+ protected :"#{name}="
end
end
def initialize(options = {})
defaults = {
@@ -68,21 +73,20 @@
:resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym,
:resolver_options => { cache: true },
}
defaults.merge!(options)
- defaults[:headers] = Headers.new(defaults[:headers])
defaults.each do |(k, v)|
__send__(:"#{k}=", v)
end
end
def_option(:headers) do |headers|
if self.headers
self.headers.merge(headers)
else
- headers
+ Headers.new(headers)
end
end
def_option(:timeout) do |opts|
Timeout.new(opts)
@@ -114,11 +118,10 @@
transport
end
%w[
- params form json body
- follow ssl http2_settings
+ params form json body ssl http2_settings
request_class response_class headers_class request_body_class response_body_class connection_class
io fallback_protocol debug debug_level transport_options resolver_class resolver_options
persistent
].each do |method_name|
def_option(method_name)