Skip to content

Commit 6f9cefd

Browse files
committed
🔎 Improve #pretty_print [🚧WIP: tests]
1 parent 99e17df commit 6f9cefd

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

lib/net/imap/config.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,33 @@ def inspect;
518518
end
519519
alias to_s inspect
520520

521+
# Used by PP[https://docs.ruby-lang.org/en/master/PP.html] to create a
522+
# string representation of all config attributes and the inheritance
523+
# chain. Inherited attributes are listed with the ancestor config from
524+
# which they are inherited.
525+
#
526+
# pp Config.new[0.4].new(open_timeout: 10, idle_response_timeout: 10)
527+
# # #<Net::IMAP::Config:0x0000745871125410
528+
# # open_timeout=10
529+
# # idle_response_timeout=10
530+
# # inherits from Net::IMAP::Config[0.4]
531+
# # responses_without_block=:silence_deprecation_warning
532+
# # max_response_size=nil
533+
# # sasl_ir=true
534+
# # enforce_logindisabled=false
535+
# # parser_use_deprecated_uidplus_data=true
536+
# # parser_max_deprecated_uidplus_data_size=1000
537+
# # inherits from Net::IMAP::Config.global
538+
# # inherits from Net::IMAP::Config.default
539+
# # debug=false>
540+
#
541+
# Related: #inspect, #to_h.
542+
def pretty_print(pp)
543+
pp.group(2, "#<", ">") do
544+
pretty_print_recursive(pp)
545+
end
546+
end
547+
521548
# :stopdoc:
522549

523550
protected
@@ -554,6 +581,26 @@ def inspect_recursive(attrs = AttrAccessors.struct.members)
554581
strings.join " "
555582
end
556583

584+
def pretty_print_recursive(pp, attrs = AttrAccessors.struct.members)
585+
pp.text name
586+
assigned = assigned_attrs_hash(attrs)
587+
pp.breakable
588+
pp.seplist(assigned, ->{pp.breakable}) do |key, val|
589+
pp.text key.to_s
590+
pp.text "="
591+
pp.pp val
592+
end
593+
if parent
594+
pp.breakable if assigned.any?
595+
pp.nest(2) do
596+
pp.text "inherits from "
597+
parent.pretty_print_recursive(pp, attrs - assigned.keys)
598+
end
599+
elsif assigned.empty?
600+
pp.text "(overridden)"
601+
end
602+
end
603+
557604
def assigned_attrs_hash(attrs)
558605
own_attrs = attrs.reject { inherited?(_1) }
559606
own_attrs.to_h { [_1, data[_1]] }

0 commit comments

Comments
 (0)