We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
ESearchResult#each
1 parent 82763e1 commit cd154d8Copy full SHA for cd154d8
2 files changed
lib/net/imap/esearch_result.rb
@@ -45,7 +45,7 @@ def initialize(tag: nil, uid: nil, data: nil)
45
# Note that SearchResult also implements +to_a+, so it can be used without
46
# checking if the server returned +SEARCH+ or +ESEARCH+ data.
47
#
48
- # Related: #to_sequence_set, #all, #partial
+ # Related: #each, #to_sequence_set, #all, #partial
49
def to_a; to_sequence_set.numbers end
50
51
# :call-seq: to_sequence_set -> SequenceSet or nil
@@ -61,11 +61,28 @@ def to_a; to_sequence_set.numbers end
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
+ # Related: #each, #to_a, #all, #partial
65
def to_sequence_set
66
all || partial&.to_sequence_set || SequenceSet.empty
67
end
68
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
+
86
##
87
# attr_reader: tag
88
test/net/imap/test_esearch_result.rb
@@ -34,6 +34,28 @@ class ESearchResultTest < Test::Unit::TestCase
34
assert_equal [1, 5, 6, 7, 8], esearch.to_a
35
36
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"]]])
+ assert_equal [1, 5, 6, 7, 8], esearch.each.to_a
+ assert_equal [1, 5, 6, 7, 8], ary
+ esearch = ESearchResult.new(nil, false, [
+ ["PARTIAL", ESearchResult::PartialResult[1..5, "1,5:8"]]
52
+ ])
53
54
55
56
57
58
59
test "#tag" do
60
esearch = ESearchResult.new("A0001", false, [["count", 0]])
assert_equal "A0001", esearch.tag
0 commit comments