lib/httpx/options.rb in httpx-0.16.1 vs lib/httpx/options.rb in httpx-0.17.0

- old
+ new

@@ -37,10 +37,32 @@ :persistent => false, :resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym, :resolver_options => { cache: true }, }.freeze + begin + module HashExtensions + refine Hash do + def >=(other) + Hash[other] <= self + end + + def <=(other) + other = Hash[other] + return false unless size <= other.size + + each do |k, v| + v2 = other.fetch(k) { return false } + return false unless v2 == v + end + true + end + end + end + using HashExtensions + end unless Hash.method_defined?(:>=) + class << self def new(options = {}) # let enhanced options go through return options if self == Options && options.class < self return options if options.is_a?(self) @@ -87,11 +109,11 @@ end end def initialize(options = {}) defaults = DEFAULT_OPTIONS.merge(options) - defaults.each do |(k, v)| + defaults.each do |k, v| next if v.nil? begin value = __send__(:"option_#{k}", v) instance_variable_set(:"@#{k}", value) @@ -161,10 +183,11 @@ ].each do |method_name| def_option(method_name) end REQUEST_IVARS = %i[@params @form @json @body].freeze + private_constant :REQUEST_IVARS def ==(other) ivars = instance_variables | other.instance_variables ivars.all? do |ivar| case ivar @@ -178,18 +201,18 @@ end end end def merge(other) - raise ArgumentError, "#{other.inspect} is not a valid set of options" unless other.respond_to?(:to_hash) + raise ArgumentError, "#{other} is not a valid set of options" unless other.respond_to?(:to_hash) h2 = other.to_hash return self if h2.empty? h1 = to_hash - return self if h1 == h2 + return self if h1 >= h2 merged = h1.merge(h2) do |_k, v1, v2| if v1.respond_to?(:merge) && v2.respond_to?(:merge) v1.merge(v2) else @@ -199,13 +222,12 @@ self.class.new(merged) end def to_hash - hash_pairs = instance_variables.map do |ivar| - [ivar[1..-1].to_sym, instance_variable_get(ivar)] + instance_variables.each_with_object({}) do |ivar, hs| + hs[ivar[1..-1].to_sym] = instance_variable_get(ivar) end - Hash[hash_pairs] end if RUBY_VERSION > "2.4.0" def initialize_dup(other) instance_variables.each do |ivar|