Skip to content

Commit 85957ff

Browse files
committed
🔍 Improve Config#pretty_print (for Kernel::pp)
Viewing config objects in Irb was _much_ too messy to easily inspect: it printed every attribute for each ancestor, and most of them would simply be the Net::IMAP::Config::AttrInheritance::INHERITED const, repeated over and over. So, just like `Config#inspect` was improved by #546, this simplifies the `Kernel#pp` output to make it more useful for inspection on the console. Unlike `Config#inspect`, `Config#pretty_print` prints _every_ config attribute, including attributes that have been defined by a named default config. Like `Config#inspect`, each attributes is printed only once, nested in the config which assigned it.
1 parent 6093acb commit 85957ff

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

lib/net/imap/config.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,42 @@ def to_h; data.members.to_h { [_1, send(_1)] } end
523523
# # inherits from Net::IMAP::Config.global debug=true
524524
# # inherits from Net::IMAP::Config.default>"
525525
#
526+
# Use +pp+ (see #pretty_print) to inspect _all_ config attributes with
527+
# the _full_ inheritance chain.
528+
#
526529
# Use #to_h to inspect all config attributes ignoring inheritance.
527530
def inspect;
528531
"#<#{inspect_recursive}>"
529532
end
530533
alias to_s inspect
531534

535+
# Used by PP[https://docs.ruby-lang.org/en/master/PP.html] to create a
536+
# string representation of all config attributes and the inheritance
537+
# chain. Inherited attributes are listed with the ancestor config from
538+
# which they are inherited.
539+
#
540+
# pp Config.new[0.4].new(open_timeout: 10, idle_response_timeout: 10)
541+
# # #<Net::IMAP::Config:0x0000745871125410
542+
# # open_timeout=10
543+
# # idle_response_timeout=10
544+
# # inherits from Net::IMAP::Config[0.4]
545+
# # responses_without_block=:silence_deprecation_warning
546+
# # max_response_size=nil
547+
# # sasl_ir=true
548+
# # enforce_logindisabled=false
549+
# # parser_use_deprecated_uidplus_data=true
550+
# # parser_max_deprecated_uidplus_data_size=1000
551+
# # inherits from Net::IMAP::Config.global
552+
# # inherits from Net::IMAP::Config.default
553+
# # debug=false>
554+
#
555+
# Related: #inspect, #to_h.
556+
def pretty_print(pp)
557+
pp.group(2, "#<", ">") do
558+
pretty_print_recursive(pp)
559+
end
560+
end
561+
532562
# :stopdoc:
533563

534564
protected
@@ -566,6 +596,26 @@ def inspect_recursive(attrs = AttrAccessors.struct.members)
566596
strings.join " "
567597
end
568598

599+
def pretty_print_recursive(pp, attrs = AttrAccessors.struct.members)
600+
pp.text name
601+
assigned = assigned_attrs_hash(attrs)
602+
pp.breakable
603+
pp.seplist(assigned, ->{pp.breakable}) do |key, val|
604+
pp.text key.to_s
605+
pp.text "="
606+
pp.pp val
607+
end
608+
if parent
609+
pp.breakable if assigned.any?
610+
pp.nest(2) do
611+
pp.text "inherits from "
612+
parent.pretty_print_recursive(pp, attrs - assigned.keys)
613+
end
614+
elsif assigned.empty?
615+
pp.text "(overridden)"
616+
end
617+
end
618+
569619
def assigned_attrs_hash(attrs)
570620
own_attrs = attrs.reject { inherited?(_1) }
571621
own_attrs.to_h { [_1, data[_1]] }

test/net/imap/test_config.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,33 @@ def config_id(config)
484484
)
485485
end
486486

487+
test "#pretty_print" do
488+
default_attrs = Config.global.to_h.map { "%s=%p" % _1 }
489+
width = 80
490+
output = String.new
491+
PP.pp Config.global, output, width
492+
assert_equal(<<~PP, output)
493+
#<#{Config}.global
494+
inherits from #{Config}.default
495+
#{default_attrs.join("\n ")}>
496+
PP
497+
498+
Config.global.debug = true
499+
config = Config[0.5].new(enforce_logindisabled: :when_capabilities_cached)
500+
default_attrs = Config[0.5].to_h.except(:debug, :enforce_logindisabled)
501+
.map { "%s=%p" % _1 }
502+
503+
output = String.new
504+
PP.pp config, output, width
505+
assert_equal(<<~PP, output)
506+
#<#{Config}:0x#{config_id(config)}
507+
enforce_logindisabled=:when_capabilities_cached
508+
inherits from #{Config}[0.5]
509+
#{default_attrs.join("\n ")}
510+
inherits from #{Config}.global
511+
debug=true
512+
inherits from #{Config}.default
513+
(overridden)>
514+
PP
515+
end
487516
end

0 commit comments

Comments
 (0)