Skip to content

Commit cd154d8

Browse files
committed
✨ Add ESearchResult#each
`ESearchResult#each` allows `ESearchResult` to be used for iteration over UIDs or message sequence numbers, just like `SearchResult`.
1 parent 82763e1 commit cd154d8

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

lib/net/imap/esearch_result.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def initialize(tag: nil, uid: nil, data: nil)
4545
# Note that SearchResult also implements +to_a+, so it can be used without
4646
# checking if the server returned +SEARCH+ or +ESEARCH+ data.
4747
#
48-
# Related: #to_sequence_set, #all, #partial
48+
# Related: #each, #to_sequence_set, #all, #partial
4949
def to_a; to_sequence_set.numbers end
5050

5151
# :call-seq: to_sequence_set -> SequenceSet or nil
@@ -61,11 +61,28 @@ def to_a; to_sequence_set.numbers end
6161
# Note that SearchResult also implements +to_sequence_set+, so it can be
6262
# used without checking if the server returned +SEARCH+ or +ESEARCH+ data.
6363
#
64-
# Related: #to_a, #all, #partial
64+
# Related: #each, #to_a, #all, #partial
6565
def to_sequence_set
6666
all || partial&.to_sequence_set || SequenceSet.empty
6767
end
6868

69+
# When either #all or #partial contains a SequenceSet of message sequence
70+
# numbers or UIDs, +each+ yields each integer in the set.
71+
#
72+
# When both #all and #partial are +nil+, either because the server
73+
# returned no results or because +ALL+ and +PARTIAL+ were not included in
74+
# the IMAP#search +RETURN+ options, #each does not yield.
75+
#
76+
# Note that SearchResult also implements +#each+, so it can be used
77+
# without checking if the server returned +SEARCH+ or +ESEARCH+ data.
78+
#
79+
# Related: #to_sequence_set, #to_a, #all, #partial
80+
def each(&)
81+
return to_enum(__callee__) unless block_given?
82+
to_sequence_set.each_number(&)
83+
self
84+
end
85+
6986
##
7087
# attr_reader: tag
7188
#

test/net/imap/test_esearch_result.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ class ESearchResultTest < Test::Unit::TestCase
3434
assert_equal [1, 5, 6, 7, 8], esearch.to_a
3535
end
3636

37+
test "#each" do
38+
esearch = ESearchResult.new(nil, true, [])
39+
assert_kind_of Enumerator, esearch.each
40+
ary = []
41+
assert_same esearch, esearch.each { ary << _1 }
42+
assert_equal [], ary
43+
44+
esearch = ESearchResult.new(nil, false, [["ALL", SequenceSet["1,5:8"]]])
45+
assert_equal [1, 5, 6, 7, 8], esearch.each.to_a
46+
ary = []
47+
assert_same esearch, esearch.each { ary << _1 }
48+
assert_equal [1, 5, 6, 7, 8], ary
49+
50+
esearch = ESearchResult.new(nil, false, [
51+
["PARTIAL", ESearchResult::PartialResult[1..5, "1,5:8"]]
52+
])
53+
assert_equal [1, 5, 6, 7, 8], esearch.each.to_a
54+
ary = []
55+
assert_same esearch, esearch.each { ary << _1 }
56+
assert_equal [1, 5, 6, 7, 8], ary
57+
end
58+
3759
test "#tag" do
3860
esearch = ESearchResult.new("A0001", false, [["count", 0]])
3961
assert_equal "A0001", esearch.tag

0 commit comments

Comments
 (0)