Skip to content

Commit 5ac99b2

Browse files
committed
✨ Responses with no args can return frozen dup hash
This adds a new `:frozen_dup` option to `config.responses_without_block` which allows it to return a frozen copy of the responses hash, with each response type array also being a frozen copy.
1 parent 1a25273 commit 5ac99b2

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

lib/net/imap.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2492,11 +2492,13 @@ def idle_done
24922492

24932493
RESPONSES_DEPRECATION_MSG =
24942494
"Pass a type or block to #responses, " \
2495-
"set config.responses_without_block to :silence_deprecation_warning, " \
2495+
"set config.responses_without_block to :frozen_dup " \
2496+
"or :silence_deprecation_warning, " \
24962497
"or use #extract_responses or #clear_responses."
24972498
private_constant :RESPONSES_DEPRECATION_MSG
24982499

24992500
# :call-seq:
2501+
# responses -> hash of {String => Array} (see config.responses_without_block)
25002502
# responses(type) -> frozen array
25012503
# responses {|hash| ...} -> block result
25022504
# responses(type) {|array| ...} -> block result
@@ -2526,6 +2528,10 @@ def idle_done
25262528
# Prints a warning and returns the mutable responses hash.
25272529
# <em>This is not thread-safe.</em>
25282530
#
2531+
# [+:frozen_dup+</em>]
2532+
# Returns a frozen copy of the unhandled responses hash, with frozen
2533+
# array values.
2534+
#
25292535
# [+:raise+ <em>(planned future default)</em>]
25302536
# Raise an +ArgumentError+ with the deprecation warning.
25312537
#
@@ -2597,6 +2603,13 @@ def responses(type = nil)
25972603
raise ArgumentError, RESPONSES_DEPRECATION_MSG
25982604
when :warn
25992605
warn(RESPONSES_DEPRECATION_MSG, uplevel: 1, category: :deprecated)
2606+
when :frozen_dup
2607+
synchronize {
2608+
responses = @responses.transform_values(&:freeze)
2609+
responses.default_proc = nil
2610+
responses.default = [].freeze
2611+
return responses.freeze
2612+
}
26002613
end
26012614
@responses
26022615
end

lib/net/imap/config.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@ def self.[](config)
260260
# Prints a warning and returns the mutable responses hash.
261261
# <em>This is not thread-safe.</em>
262262
#
263+
# [+:frozen_dup+</em>]
264+
# Returns a frozen copy of the unhandled responses hash, with frozen
265+
# array values.
266+
#
267+
# Note that calling IMAP#responses with a +type+ and without a block is
268+
# not configurable and always behaves like +:frozen_dup+.
269+
#
270+
# <em>(+:frozen_dup+ config option was added in +v0.4.17+)</em>
271+
#
263272
# [+:raise+ <em>(planned future default)</em>]
264273
# Raise an ArgumentError with the deprecation warning.
265274
#

test/net/imap/test_imap_responses.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ def assert_responses_warn
166166
assert_equal [], imap.responses["FAKE"]
167167
end
168168
assert_empty stderr
169+
# opt-in to future behavior
170+
imap.config.responses_without_block = :frozen_dup
171+
stderr = EnvUtil.verbose_warning do
172+
assert imap.responses.frozen?
173+
assert imap.responses["CAPABILITY"].frozen?
174+
assert_equal(%w[IMAP4REV1 NAMESPACE MOVE IDLE UTF8=ACCEPT],
175+
imap.responses["CAPABILITY"].last)
176+
end
177+
assert_empty stderr
169178
end
170179
end
171180

0 commit comments

Comments
 (0)