mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Ruby 2.6, 2.7 and 3 fixes (#405)
This commit is contained in:
parent
0a681f47ea
commit
c28d43253c
5 changed files with 29 additions and 12 deletions
|
@ -1,4 +1,5 @@
|
|||
## master (unreleased)
|
||||
- Tests work with Ruby 2.6.6, 2.7.2 and 3.0.0
|
||||
- Update awsome_print.gemspec nokogiri dependency to resolve CVEs [@gvwirth]
|
||||
|
||||
## 1.9.0
|
||||
|
|
|
@ -106,21 +106,29 @@ module AwesomePrint
|
|||
|
||||
# Utility methods.
|
||||
#------------------------------------------------------------------------------
|
||||
def convert_to_hash(object)
|
||||
if !object.respond_to?(:to_hash)
|
||||
return nil
|
||||
end
|
||||
|
||||
if object.method(:to_hash).arity != 0
|
||||
return nil
|
||||
end
|
||||
# A class (ex. `Net::HTTP.Get`) might have `attr_reader :method` accessor
|
||||
# which causes `object.method(:to_hash)` throw `ArgumentError (wrong number
|
||||
# of arguments (given 1, expected 0))`. The following tries to avoid that.
|
||||
def has_method_accessor?(object)
|
||||
!object.method(:method)
|
||||
rescue ArgumentError
|
||||
true
|
||||
end
|
||||
|
||||
def convert_to_hash(object)
|
||||
return nil if has_method_accessor?(object)
|
||||
return nil if !object.respond_to?(:to_hash) || object.method(:to_hash).arity != 0
|
||||
|
||||
# ActionController::Parameters will raise if they are not yet permitted
|
||||
# and we try to convert to hash.
|
||||
# https://api.rubyonrails.org/classes/ActionController/Parameters.html
|
||||
return nil if object.respond_to?(:permitted?) && !object.permitted?
|
||||
|
||||
hash = object.to_hash
|
||||
if !hash.respond_to?(:keys) || !hash.respond_to?('[]')
|
||||
return nil
|
||||
end
|
||||
return nil if !hash.respond_to?(:keys) || !hash.respond_to?(:[])
|
||||
|
||||
return hash
|
||||
hash
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -90,7 +90,7 @@ module AwesomePrint
|
|||
# #<Method: User(id: integer, username: string)(ActiveRecord::Base).current>
|
||||
# #<UnboundMethod: Hello#world>
|
||||
#
|
||||
if method.to_s =~ /(Unbound)*Method: (.*)[#\.]/
|
||||
if method.to_s =~ /(Unbound)*Method: (.*?)[#\.]/
|
||||
unbound = $1 && '(unbound)'
|
||||
klass = $2
|
||||
if klass && klass =~ /(\(\w+:\s.*?\))/ # Is this ActiveRecord-style class?
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
require 'net/http'
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe 'AwesomePrint' do
|
||||
|
@ -72,6 +73,12 @@ RSpec.describe 'AwesomePrint' do
|
|||
end
|
||||
expect { weird.new.ai }.not_to raise_error
|
||||
end
|
||||
|
||||
it 'handles attr_reader :method' do
|
||||
uri = URI.parse('https://hello.nx/world')
|
||||
req = Net::HTTP::Get.new(uri)
|
||||
expect(req.ai(plain: true)).to eq('#<Net::HTTP::Get GET>')
|
||||
end
|
||||
end
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
|
|
|
@ -37,6 +37,7 @@ ExtVerifier.require_dependencies!(
|
|||
)
|
||||
)
|
||||
require 'nokogiri'
|
||||
require 'ostruct'
|
||||
require 'awesome_print'
|
||||
|
||||
RSpec.configure do |config|
|
||||
|
|
Loading…
Add table
Reference in a new issue