Skip to content

Commit 93ec2c3

Browse files
committed
✨ Add ESearchResult#to_sequence_set
Similar to `SearchResult`, this method allows ESearchResult to be used as an argument where a SequenceSet is required, such as with `#fetch`. This also updates `ESearchResult#to_a` to use `to_sequence_set.numbers`.
1 parent d027cd2 commit 93ec2c3

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

lib/net/imap/esearch_result.rb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,32 @@ def initialize(tag: nil, uid: nil, data: nil)
3939
# numbers or UIDs, +to_a+ returns that set as an array of integers.
4040
#
4141
# When both #all and #partial are +nil+, either because the server
42-
# returned no results or because +ALL+ and +PARTIAL+ were not included in
43-
# the IMAP#search +RETURN+ options, #to_a returns an empty array.
42+
# returned no results or because neither +ALL+ or +PARTIAL+ were included
43+
# in the IMAP#search +RETURN+ options, #to_a returns an empty array.
4444
#
4545
# Note that SearchResult also implements +to_a+, so it can be used without
4646
# checking if the server returned +SEARCH+ or +ESEARCH+ data.
47-
def to_a; all&.numbers || partial&.to_a || [] end
47+
#
48+
# Related: #to_sequence_set, #all, #partial
49+
def to_a; to_sequence_set.numbers end
50+
51+
# :call-seq: to_sequence_set -> SequenceSet or nil
52+
#
53+
# When either #all or #partial contains a SequenceSet of message sequence
54+
# numbers or UIDs, +to_sequence_set+ returns that sequence set.
55+
#
56+
# When both #all and #partial are +nil+, either because the server
57+
# returned no results or because neither +ALL+ or +PARTIAL+ were included
58+
# in the IMAP#search +RETURN+ options, #to_sequence_set returns
59+
# SequenceSet.empty.
60+
#
61+
# Note that SearchResult also implements +to_sequence_set+, so it can be
62+
# used without checking if the server returned +SEARCH+ or +ESEARCH+ data.
63+
#
64+
# Related: #to_a, #all, #partial
65+
def to_sequence_set
66+
all || partial&.to_sequence_set || SequenceSet.empty
67+
end
4868

4969
##
5070
# attr_reader: tag
@@ -161,6 +181,8 @@ def initialize(range:, results:)
161181
#
162182
# See also: ESearchResult#to_a.
163183
def to_a; results&.numbers || [] end
184+
185+
alias to_sequence_set results
164186
end
165187

166188
# :call-seq: partial -> PartialResult or nil

test/net/imap/test_esearch_result.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ class ESearchResultTest < Test::Unit::TestCase
88
SequenceSet = Net::IMAP::SequenceSet
99
ExtensionData = Net::IMAP::ExtensionData
1010

11+
test "#to_sequence_set" do
12+
esearch = ESearchResult.new(nil, true, [])
13+
assert_equal SequenceSet.empty, esearch.to_sequence_set
14+
esearch = ESearchResult.new(nil, false, [])
15+
assert_equal SequenceSet.empty, esearch.to_sequence_set
16+
esearch = ESearchResult.new(nil, false, [["ALL", SequenceSet["1,5:8"]]])
17+
assert_equal SequenceSet[1, 5, 6, 7, 8], esearch.to_sequence_set
18+
esearch = ESearchResult.new(nil, false, [
19+
["PARTIAL", ESearchResult::PartialResult[1..5, "1,5:8"]]
20+
])
21+
assert_equal SequenceSet[1, 5, 6, 7, 8], esearch.to_sequence_set
22+
end
23+
1124
test "#to_a" do
1225
esearch = ESearchResult.new(nil, true, [])
1326
assert_equal [], esearch.to_a

0 commit comments

Comments
 (0)